I am getting this error while updating the same document. I am continuously getting data from the socket and updates the document. But recent data was yet not updated and on socket emit updating new data on the same document. So how could I handle this error or wait for the callback for that document?
{ 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' }
Here is snippet
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);
}
}
});
The error message is hinting it cannot find the document, so if it's a native query try wrapping it with ObjectId
, ie ObjectId("507f1f77bcf86cd799439011")
.
To answer your question as to how you can wait for the document or handle the error depends upon your db driver. For instance, if you are using promised-mongo:
db.collection.update(...).then((doc) => {
// This will run once updated
}).catch((err) => { //handle the error});
read more: https://github.com/gordonmleigh/promised-mongo
Or if you're using the mongodb npm module:
db.collection.update({_id: ObjectId('yourId'), {updateKey:
'updateVal}, (err,val) => {
if(err) //handle it and return
// update finished
return callback(doc);
});
more details at https://www.npmjs.com/package/mongodb
If the problem still exists, try setting the query with a flag {upsert: true}
, as described here
Keep in mind incoming socket messages might not always arrive in the same order they were sent, handle the state appropriately or use a queue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With