The argument must be a string in nodejs

I have the following code:

var objectid = infos[i].id; var name = infos[i].name; return collection.aggregate([ {$match: {app: new ObjectId(objectid)}}, {$group: {_id: "$uid", amt: {$sum: 1}}} ]) 

This code used to work fine before, but lately I started getting the bottom stack in sails:

 error: TypeError: Argument must be a string at TypeError (native) at Buffer.write (buffer.js:791:21) at serializeObjectId (/Users/user/git/pivot/code/node_modules/sails-mongo/node_modules/bson/lib/bson/parser/serializer.js:242:10) at serializeInto (/Users/user/git/pivot/code/node_modules/sails-mongo/node_modules/bson/lib/bson/parser/serializer.js:699:17) at serializeObject (/Users/user/git/pivot/code/node_modules/sails-mongo/node_modules/bson/lib/bson/parser/serializer.js:280:18) at serializeInto (/Users/user/git/pivot/code/node_modules/sails-mongo/node_modules/bson/lib/bson/parser/serializer.js:705:17) at serializeObject (/Users/user/git/pivot/code/node_modules/sails-mongo/node_modules/bson/lib/bson/parser/serializer.js:280:18) at serializeInto (/Users/user/git/pivot/code/node_modules/sails-mongo/node_modules/bson/lib/bson/parser/serializer.js:551:17) at serializeObject (/Users/user/git/pivot/code/node_modules/sails-mongo/node_modules/bson/lib/bson/parser/serializer.js:280:18) at serializeInto (/Users/user/git/pivot/code/node_modules/sails-mongo/node_modules/bson/lib/bson/parser/serializer.js:705:17) at serialize (/Users/user/git/pivot/code/node_modules/sails-mongo/node_modules/bson/lib/bson/bson.js:47:27) 

It complains about the ObjectId , which I imported like this:

 var ObjectId = require('mongodb').ObjectID; 

As I said, this works fine, but no longer works. I'm really confused. If I put objectId in a string, it will not return any results. If I leave it as it is (since it worked before), it throws exceptions. What is the problem?

I read below:

https://docs.mongodb.com/v3.0/reference/operator/aggregation/cmp/#exp._S_cmp

I can do it in robomongo:

  db.getCollection("openevent").aggregate([ {$match: {app: new ObjectId(OBJECT_ID) }}, {$group: {_id: "$uid", amt: {$sum: 1}}} ]) 

Using the same values ​​as above. What am I doing wrong?

I see the following for sails-mongo :

 └─┬ sails-mongo@0.12.1 └─┬ mongodb@2.1.6 └── mongodb-core@1.3.1 
+6
source share
2 answers

For posterity, this is usually due to compatibility issues between Mongo versions. The MongoDB 2.2 driver uses mongodb-core 2.0 (and bson 0.5), while the MongoDB driver 2.1 uses mongodb-core 1.3 and bson 0.4. If you try to use the MongoDB 2.1 ObjectId driver with the MongoDB 2.2 driver, you will get this error.

+5
source

I have no idea why this is even an answer, but I will post it.

I used to have this:

 var ObjectId = require('mongodb').ObjectID; 

I changed to this:

 var ObjectId = require('sails-mongo/node_modules/mongodb').ObjectID; 

And somehow it all changed.

+11
source

All Articles