mongo-mock
mongo-mock copied to clipboard
bug: findOneAndUpdate throws ModifyJsError error if no records found for given filter
I tried to mock one of my findOneAndUpdate
queries that are working on my mongodb instance, and I've encountered this ModifyJsError
error...
Expected result:
If no records were found by filter object, the result of findOneAndUpdate
function returns null;
Actual result:
findOneAndUpdate
throws ModifyJsError
error if no records were found by the filter object.
Sample code:
var mongodb = require('mongo-mock');
mongodb.max_delay = 0;//you can choose to NOT pretend to be async (default is 400ms)
var MongoClient = mongodb.MongoClient;
MongoClient.persist = 'mongo.js'
var ObjectID = require('bson-objectid');
var url = 'mongodb://localhost:27017/myproject';
MongoClient.connect(url, {}, async function(err, db) {
var collection = db.collection('documents');
collection.insert({ lastStatus: 'not-pending'}); // <------ Non 'pending' status
const queryFilter = { lastStatus: 'pending' };
const queryUpdate = {
$set: {
lastStatus: 'processing',
lastUpdated: Date.now()
}
};
const res = await collection.findOneAndUpdate(queryFilter, queryUpdate);
console.log(res);
});
Cli output:
└─▪ DEBUG=* node ./findOneAndUpdate-index.js
mongo-mock:mongo_client connecting localhost:27017/myproject +0ms
mongo-mock:collection initializing instance of `system.namespaces` with 1 documents +2ms
mongo-mock:collection initializing instance of `system.indexes` with 0 documents +1ms
mongo-mock:db myproject open +1ms
mongo-mock:collection initializing instance of `documents` with undefined documents +0ms
mongo-mock:collection insert {"lastStatus":"not-pending"} +1ms
mongo-mock:collection documents.update: {"lastStatus":"pending"} +0ms
mongo-mock:db documents persist() - creating _id index +2ms
mongo-mock:mongo_client persisting to mongo.js +1ms
mongo-mock:collection documents.update: [] +1ms
mongo-mock:collection documents.update: checking for index conflicts +0ms
/home/pavel/git-repos/temp/mongo-mock-bug/node_modules/modifyjs/dist/bundle.js:252
throw e;
^
ModifyJsError: cannot use the part 'lastStatus' to traverse undefined
at ModifyJsError (/home/pavel/git-repos/temp/mongo-mock-bug/node_modules/modifyjs/dist/bundle.js:141:11)
at findModTarget (/home/pavel/git-repos/temp/mongo-mock-bug/node_modules/modifyjs/dist/bundle.js:250:15)
at /home/pavel/git-repos/temp/mongo-mock-bug/node_modules/modifyjs/dist/bundle.js:197:22
at /home/pavel/git-repos/temp/mongo-mock-bug/node_modules/modifyjs/dist/bundle.js:68:5
at Array.forEach (<anonymous>)
at Object.each (/home/pavel/git-repos/temp/mongo-mock-bug/node_modules/modifyjs/dist/bundle.js:67:32)
at /home/pavel/git-repos/temp/mongo-mock-bug/node_modules/modifyjs/dist/bundle.js:187:9
at /home/pavel/git-repos/temp/mongo-mock-bug/node_modules/modifyjs/dist/bundle.js:68:5
at Array.forEach (<anonymous>)
at Object.each (/home/pavel/git-repos/temp/mongo-mock-bug/node_modules/modifyjs/dist/bundle.js:67:32)
node version: 10.15.1 npm version: 6.4.1
Can this be a duplicate of https://github.com/williamkapke/mongo-mock/issues/107 ?