Skip to content Skip to sidebar Skip to footer

Mongoose 'findbyid' Returns Null With Valid Id

EDIT [SOLVED]: I was connecting to the wrong database... I changed var dbURI = 'mongodb://localhost/wifiplz' to var dbURI = 'mongodb://localhost/wifiPlz' so all of this was due to

Solution 1:

Dear,

make following changes :

var mongoose = require('mongoose');
varLocation = mongoose.model('Location');

module.exports.locationRead = function(req, res) {
  Location
    .findOne({_id: req.params.locationId}, function (err, location){
      if (err) throw err;
      res.status(200);
      res.json(location); // returns null
    });
}

_id could be your any field so replace your db field with _id but make sure that field should be primary in nature or unique. If it's not create an index over that field

Thanks & Cheers

Post a Comment for "Mongoose 'findbyid' Returns Null With Valid Id"