node.js - Mongoose findByID return no error when id not set -


this question has answer here:

i have collection id (_id).

{   "_id": objectid("5583df9da55f654609901eed"),   "group": "tomorrowland",   "description": "délire 2015 à tomorrowland",   "uid": [     "5583df21a55f654609901eec",     "5583e8ef9aeb31390bb50bf6"   ],   "pictures": [     "1434705960113.jpg",     "1434705974710.jpg",     "1434706147177.jpg",     "1434711007201.jpg"   ],   "__v": 0 } 

if make query (findbyid) , check id 1 error (all work fine in case : id not exist)

now if make same query , check id (5582a7f12686cf776a0daee5), no error ... id doen't exist in collection !

how possible ?

here code :

groupid = req.headers['groupid'];  cloud.findbyid(groupid).exec(function(err, data) {   if(err){     res.status(404);     res.json('group not found');   }   else   {     // stuff   } }); 

you need convert string _id valid mongodb id

var mongo = require('mongodb'),     bson = mongo.bsonpure,     groupid = new bson.objectid(req.headers['groupid']);  cloud.find({'_id': groupid}).exec(function(err, data) {   if(err){     res.status(404);     res.json('group not found');   }   else   {     // stuff   } }); 

if use mongoose can convert string _id as:

var mongoose = require('mongoose'),     groupid = mongoose.types.objectid(req.headers['groupid']); 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -