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' }
1 Answers
Answers 1
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.
0 comments:
Post a Comment