最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - node + mongo: updating a record requires a callback - Stack Overflow

programmeradmin8浏览0评论

So I'm listening for an event with socket.io, once that fires I'm trying to update a record to a new value.

socket.on('contentEdited', function (newContent) {

collection.update(
    { '_id' : ObjectId("5279262e74d92da751eb2b8e") }, 
    { $set: { 
      'content': newContent
      } 
    }
  ), function (err, result) {
    if (err) throw err;
    console.log(result)
  };

});

The syntax works in the shell, but throws the following error in node when the event fires:

Error: Cannot use a writeConcern without a provided callback

I tried adding an function at the end afterwards for basic error checking, but I'm not sure how to provide a callback in the way mongo expects.

Still kinda new to this, thanks

So I'm listening for an event with socket.io, once that fires I'm trying to update a record to a new value.

socket.on('contentEdited', function (newContent) {

collection.update(
    { '_id' : ObjectId("5279262e74d92da751eb2b8e") }, 
    { $set: { 
      'content': newContent
      } 
    }
  ), function (err, result) {
    if (err) throw err;
    console.log(result)
  };

});

The syntax works in the shell, but throws the following error in node when the event fires:

Error: Cannot use a writeConcern without a provided callback

I tried adding an function at the end afterwards for basic error checking, but I'm not sure how to provide a callback in the way mongo expects.

Still kinda new to this, thanks

Share Improve this question asked Nov 6, 2013 at 4:24 PirijanPirijan 3,5793 gold badges20 silver badges29 bronze badges 2
  • 1 Don't know much about mongo, but a quick google of write concerns yielded: docs.mongodb/manual/core/write-concern – Vinay Commented Nov 6, 2013 at 4:28
  • Yeah I read that, but I need strong write concern for this use case and the success callback would be useful. – Pirijan Commented Nov 6, 2013 at 13:41
Add a ment  | 

2 Answers 2

Reset to default 17

I think your problem is that the callback function needs to be inside the update function call instead of outside it. The format for the nodejs MongoDB driver can be found here: http://mongodb.github.io/node-mongodb-native/api-generated/collection.html#update

So it should look like this:

collection.update(
   { '_id' : ObjectId("5279262e74d92da751eb2b8e") }, 
   { $set: { 'content': newContent } },
   function (err, result) {
      if (err) throw err;
      console.log(result);
   })

Note that the parentheses has moved after the callback function.

You could also set the write concern to "unacknowledged" instead of "acknowledged."

The MongoDB concept of "Write Concerns" determines how certain you want to be that MongoDB successfully wrote to the DB. The lowest level of write concern, "Unacknowledged" just writes data to the server and doesn't wait to response. This used to be the default, but now the default is to wait for MongoDB to acknowledge the write.

You can learn more about write concerns here: http://docs.mongodb/manual/core/write-concern/

To set the write concern to unacknowledged, add the option {w: 0}:

collection.update(
   { '_id' : ObjectId("5279262e74d92da751eb2b8e") }, 
   { $set: { 'content': newContent } },
   { w : 0 });

yes. maybe you have the wrong syntax. and this might make it even better

socket.on('contentEdited', function (newContent) {

collection.update(
   { '_id' : ObjectId("5279262e74d92da751eb2b8e") }, 
   { $set: 
       { 'content': newContent } 
   },
   {returnOriginal : false},
   function (err, result) {
      if (err) throw err;
      console.log(result);
   });

})

发布评论

评论列表(0)

  1. 暂无评论