mongoose 'findById' returns null with valid id

前端 未结 1 422
南旧
南旧 2021-01-27 19:13

EDIT [SOLVED]:

I was connecting to the wrong database...

I changed

var dbURI = \'mongodb://localhost/wifiplz\'

to

相关标签:
1条回答
  • 2021-01-27 20:09

    Dear,

    make following changes :

    var mongoose = require('mongoose');
    var Location = 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

    0 讨论(0)
提交回复
热议问题