Search code examples
javascriptnode.jsmongodbpromisebluebird

How to confirm if update succeeds using mongoose and bluebird promise


I'm using bluebird and mongoose for a node page. I want to check if the update is successful before sending data back to clients via socket.js.Here's the part of the code that I can't figure out:

.then(function(a) {
    var g = collection3.update({
        _id: a.one[0]._id
    }, {
        $set: {
            avg: a.one[0].avg
        }
    }).function(err, d) {
        if (!err) {
            return 1; // Here's the problem
        }
    }) return {
    updated: g,
    info: a
};
}).then(function(c) {
    console.log(c.updated); // I can't get the `1` value
    if (c == 1) {
        io.sockets.in('index|1').emit("estimate", c.three);
    }
})

Does mongoose return a success message after update? I can't return 1 from the update query and pass it to the next then function, instead, I'm getting this object:

{ _mongooseOptions: {},
  mongooseCollection:
   { collection:
      { db: [Object],
        collectionName: 'table',
        internalHint: null,
        opts: {},
        slaveOk: false,
        serializeFunctions: false,
        raw: false,
        pkFactory: [Object],
        serverCapabilities: undefined },
     opts: { bufferCommands: true, capped: false },
     name: 'table',
     conn:....

Here's the full code:

  socket.on("input",function(d){ 
    Promise.props({
       one: collection2.aggregate([
        {
         $match:{post_id:mongoose.Types.ObjectId(d.id)}
        },
        {
         $group:{
                 _id:"$post_id",
                 avg:{$avg:"$rating"}
                }
        }
       ]).exec();
   }).then(function(a){     
      var g = collection3.update({_id:a.one[0]._id},{$set:{avg:a.one[0].avg}}).function(err,d){
        if(!err){
          return 1; // Here's the problem
        }
      })
      return {updated:g,info:a};
   }).then(function(c){
      console.log(c.updated); // I can't get the `1` value
      if(c.updated == 1){
        io.sockets.in('index|1').emit("estimate",c.three);
      }
   }).catch(function (error) {
     console.log(error);
   })

Solution

  • I'm assuming you're using Mongoose here, update() is an asynchronous function, your code is written in a synchronous style.

    Try:

       socket.on("input",function(d){ 
            Promise.props({
               one: collection2.aggregate([
                {
                 $match:{post_id:mongoose.Types.ObjectId(d.id)}
                },
                {
                 $group:{
                         _id:"$post_id",
                         avg:{$avg:"$rating"}
                        }
                }
               ]).exec()
           }).then(function(a){     
              return collection3.update({_id:a.one[0]._id},{$set:{avg:a.one[0].avg}})
              .then(function(updatedDoc){
                // if update is successful, this function will execute
    
              }, function(err){
                // if an error occured, this function will execute
    
              })
    
           }).catch(function (error) {
             console.log(error);
           })