如何处理Mongoose文档版本错误

时间:2018-05-02 14:02:35

标签: node.js mongodb mongoose

我在更新同一文档时遇到此错误。我不断从套接字获取数据并更新文档。但最近的数据尚未更新,并且套接字发出更新同一文档的新数据。那么我怎么能处理这个错误或等待该文件的回调呢?

{ Error
at model.wrappedPointCut [as save] (/var/www/html/parcel-app/node_modules/mongoose/lib/services/model/applyHooks.js:131:29)
at /var/www/html/parcel-app/server/server.js:372:22
at /var/www/html/parcel-app/node_modules/mongoose/lib/query.js:3115:18
at tryCatcher (/var/www/html/parcel-app/node_modules/bluebird/js/main/util.js:26:23)
at Promise._settlePromiseFromHandler (/var/www/html/parcel-app/node_modules/bluebird/js/main/promise.js:510:31)
at Promise._settlePromiseAt (/var/www/html/parcel-app/node_modules/bluebird/js/main/promise.js:584:18)
at Promise._settlePromises (/var/www/html/parcel-app/node_modules/bluebird/js/main/promise.js:700:14)
at Async._drainQueue (/var/www/html/parcel-app/node_modules/bluebird/js/main/async.js:123:16)
at Async._drainQueues (/var/www/html/parcel-app/node_modules/bluebird/js/main/async.js:133:10)
at Immediate.Async.drainQueues (/var/www/html/parcel-app/node_modules/bluebird/js/main/async.js:15:14)
at runCallback (timers.js:800:20)
at tryOnImmediate (timers.js:762:5)
at processImmediate [as _immediateCallback] (timers.js:733:5)
message: 'No matching document found for id "5ae99a15e5e73b39cacafe48"',
name: 'VersionError' }

这是片段

  socket.on('sendUserLocation', function(data) {
  //when app send new location of user this method will invoked
  if (data !== undefined) {
    var lat = data.lat;
    var lng = data.lng;
    var userType = data.user_type;
    var roomName = data.room_name;
    var heading = data.heading != undefined ? data.heading : 0;

    if (userType == 'db') {
      var returnBoj = {
        lat: lat,
        lng: lng,
        user_type:'db',
        room_name: roomName,
        heading: heading, //heading is degree of db boy which will -180-0-180
      };
      //finding user by room id which is user document id mongoose
      DeliveryboyModel.findById(roomName).exec(function (err, DBData) {
        if (DBData) {
          locationArray = [ parseFloat(lat) , parseFloat(lng) ];
          DBData.locationDetails.location = locationArray;
          DBData.save(function (err) {
             //getting error here
            if (err) {
              console.log(err);
            }
          });
        }
      });

      // emmiting addnewmarker so customer app will get new location and will update pin to new location
      socket.emit('addnewmarker', returnBoj);
      socket.broadcast.to(roomName).emit('addnewmarker', returnBoj);
    }
  }
});

1 个答案:

答案 0 :(得分:5)

该错误消息暗示无法找到该文档,因此,如果它是本机查询,请尝试用ObjectId(即ObjectId("507f1f77bcf86cd799439011"))包装它。

要回答有关如何等待文档或处理错误的问题,取决于您的数据库驱动程序。例如,如果您使用的是promised-mongo:

db.collection.update(...).then((doc) => {
  // This will run once updated
}).catch((err) => { //handle the error});

了解更多:https://github.com/gordonmleigh/promised-mongo

或者如果您正在使用mongodb npm模块:

db.collection.update({_id: ObjectId('yourId'), {updateKey: 
'updateVal}, (err,val) => {
  if(err) //handle it and return
  // update finished
  return callback(doc);
});

更多详细信息,请访问https://www.npmjs.com/package/mongodb

如果问题仍然存在,请尝试按照here所述用标志{upsert: true}设置查询

请记住,传入的套接字消息可能并不总是以它们发送的顺序到达,不能适当地处理状态或使用队列。