Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: findOneAndUpdate throws ModifyJsError error if no records found for given filter #108

Open
Pavel-Durov opened this issue May 27, 2019 · 1 comment

Comments

@Pavel-Durov
Copy link

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

@TanukiSharp
Copy link
Contributor

Can this be a duplicate of #107 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants