Max Baldwin
Max Baldwin

Reputation: 3452

Issues With Mongoose $push

I really just need a second set of eyes here. I am using the Mongoose npm to create a new entry in my MongoDB. Then I am using that new entry in a few functions in the Async npm.

The issue that I am having is that I am getting the first three console logs, "hitter", "create", and "req.body.campaign_id" but nothing past that. I think it has to do with my $push in the first findByIdAndUpdate. Please see my code and schema below.

Code! See async parallel "campaign" function

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);
            }
        });
    })

Campaign Schema

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' }
});

User Schema

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'}]
});

Please let me know if you need to see anything else. All help is appreciated.

Thanks!

Upvotes: 1

Views: 88

Answers (1)

Yuri Zarubin
Yuri Zarubin

Reputation: 11677

You are doing Camapaign.findByIdAndUpdate are you sure Camapaign isn't mispelled there? Shouldn't it be Campaign?

Upvotes: 1

Related Questions