Đô Nguyễn
Đô Nguyễn

Reputation: 47

Mongodb findandmodify() Cannot read property 'result' of undefined

I have problem about nodejs + mongodb findandmodify() function, last week the code below was running ok, but suddenly now it throw error:

db.collection(collectionName).findAndModify(query || {}, [], {$set: object},
     {upset: true}, callback || function () {
});

if query is undefined that code run ok, but query params ex: "_id": "idofcollection", or "template": "templatename" it throw an exception:

E:\Working\Projects\iERP\trunk\client\source\node_modules\mongodb\lib\db.js:309
handleCallback(callback, null, result.result);
                                     ^TypeError: Cannot read property 'result' of undefined
at E:\Working\Projects\iERP\trunk\client\source\node_modules\mongodb\lib\db.js:309:42
at E:\Working\Projects\iERP\trunk\client\source\node_modules\mongodb-core\lib\connection\pool.js:436:18
at doNTCallback0 (node.js:419:9)
at process._tickCallback (node.js:348:13)

Please explain for me way to solve this problem?

Upvotes: 0

Views: 725

Answers (1)

zoecarver
zoecarver

Reputation: 6403

I would use $or instead:

    db.collection(collectionName).findAndModify({ $or: [ query,  { example: 10 } ] },  {$set: object}, function (){
    });

Two things:

  1. I am not sure what callback was in your code. Usually a callback is an anonymous function.
  2. query should look something like this { price: 5 }

Here is some documentation of $or: https://docs.mongodb.com/manual/reference/operator/query/or/

Hope this helps!

Upvotes: 1

Related Questions