简体   繁体   中英

Can't find a record even when using mongodb's ObjectID in sails.js

On sailsjs and using mongodb's ObjectID object, I can't findOne() a record in the database with the native mongodb driver.

// Included the ObjectID from the mongodb dependency on sails-mongo layer

var mongodb = require("sails-mongo").mongodb;
var ObjectID = mongodb.ObjectID;    


// Inside the action2 controller...

var db = sails.getDatastore().manager;

var record = db.collection('event').findOne({_id: new ObjectID(inputs.event.id)});

// Returns an empty object
exits.success(record);

I expect to receive a json for the record in the database but I'm receiving an empty object.

It might be because you aren't getting the value of inputs.event.id or you are getting it as other data type and its not generating mongoDB objectId.

Log it and check.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM