How to handle Mongoose document version Error
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.