node.js - Mongoose $push 的问题

标签 node.js mongodb mongoose npm

我真的只需要第二双眼睛。我正在使用 Mongoose npm 在 MongoDB 中创建一个新条目。然后我在 Async npm 的几个函数中使用这个新条目。

我遇到的问题是我收到了前三个控制台日志,“hitter”、“create”和“req.body.campaign_id”,但除此之外什么都没有。我认为这与我在第一个 findByIdAndUpdate 中的 $push 有关。请参阅下面我的代码和架构。

代码!查看异步并行“事件”功能

Bid.create(req.body, function(err, bid){
        console.log('create')
        async.parallel({
            campaign: function(done) {
                console.log(req.body.campaign_id)
                Camapaign.findByIdAndUpdate(req.body.campaign_id, {
                    $push: { bids: bid._id }
                }, {
                    safe: true,
                    upsert: true
                }, function(err, campaign){
                    console.log('camp', 2)
                    if(err) {
                        console.log(err)
                        done(err)
                    } else {
                        done(null, campaign)
                    }
                });
            },
            user: function(done) {
                console.log('user', 1)
                User.findByIdAndUpdate(req.body.user_id, {
                    $push: {'bids': bid._id }
                }, {
                    safe: true,
                    upsert: true
                }, function(err, bid){
                    console.log('user', 2)
                    if(err) {
                        done(err)
                    } else {
                        done(null, bid)
                    }
                });
            }
        }, function(err, response){
            console.log('response')
            if(err) {
                console.log(err)
            } else {
                res.status(200).send(response);
            }
        });
    })

营销事件架构

var campaignSchema = new mongoose.Schema({
    title:String,
    imgUrl:[String],
    shortDesc: { type: String, set: shortenDesc },
    longDesc:String,
    duration: Number,
    price: Number,
    desired_price: Number,
    bids: [{ type: mongoose.Schema.Types.ObjectId, ref: 'bidSchema' }],
    owner_id: { type: mongoose.Schema.Types.ObjectId, ref: 'userSchema' }
});

用户架构

var schema = new mongoose.Schema({
    name: String,
    email: {
        type: String
    },
    password: {
        type: String
    },
    salt: {
        type: String
    },
    twitter: {
        id: String,
        username: String,
        token: String,
        tokenSecret: String
    },
    facebook: {
        id: String
    },
    google: {
        id: String
    },
    campaigns: [campaignSchema],
    bids: [{type: mongoose.Schema.Types.ObjectId, ref: 'bidSchema'}]
});

如果您需要查看其他内容,请告诉我。感谢所有帮助。

谢谢!

最佳答案

您正在执行Camapaign.findByIdAndUpdate,您确定Camapaign 没有拼写错误吗?难道不应该是广告事件吗?

关于node.js - Mongoose $push 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31992273/

相关文章:

node.js - 我的请求正文在 Node API 中显示为空或未定义

node.js - 在 MongoDB 中查询具有非空数组交集的文档

javascript - 使用 Mongoose 在 MongoDB 中按聚合层次进行 $match

javascript - Node HTTP 和 WS 服务器未按预期升级连接

javascript - NodeJS + MySql 请求

mongodb - 计算 mongoDB 中的不同值

mongodb - 数据库安装问题 : Binary files folder (bin) is missing in downloaded zip for MongoDB (Windows 64-bit)

node.js - 具有唯一模式键的 Mongoose 重复项

node.js - 如何在 sails 中而不是水线中使用 MongoDB

javascript - CommonJS 模块在哪里?