Node + Mongodb + ObjectId not working
I am working with nodejs+ express and mongodb.
I am using postman and access the API. When I use ObjectId the server not responding anything. If i removed means working good. I am not able to fix this issue. Please can anyone help for this.
test.js
//Post Data:
{
"list_id": "56963e4dbcd5d4ff27ced0fbd"
}
var app = require('express');
var router = app.Router();
var server = require('./../../server');
var mongoUtil = require('./../../mongoUtil');
var ObjectId = require('mongodb').ObjectID;
router.post('/share', function(req, res, next) {
var data = {
query : {}
};
console.log(req.body['list_id']);
//printed 56963e4dbcd5d4ff27ced0fbd
console.log(data.query);
//printed {}
data.query = ObjectId(req.body['list_id']);
console.log(data.query);
//Here not getting any response
// this line not printed and server no response.
//Also tried the following things. but its not working.
// data.query['_id'] = new ObjectID(req.body['list_id']);
//data.query._id = ObjectId(req.body['list_id']);
var collection = mongoUtil.list;
collection.findOne(data.query, function(err, list) {
console.log(err);
console.log(list);
if (!err && list) {
res.send("Sucess");
return;
} else {
res.send("Error");
return;
}
});
});
Solution 1:
56963e4dbcd5d4ff27ced0fbd
should be of length 24. But it is 25. Make sure it is 24. It might work.
ObjectID()
Constructor
Create a new ObjectID instance
class ObjectID()
Arguments:
id (string) – Can be a 24 byte hex string, 12 byte binary string or a Number.
Returns:
object instance of ObjectID