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 a typo (uncapitalized p). Anyone with this type of problem make sure you are connecting to the right database!


Here is my schema file (location.js):

var mongoose = require('mongoose');

var openingTimeSchema = new mongoose.Schema({
  days: {type: String, required: true},
  opening: String,
  closing: String,
  closed: {type: Boolean, required: true}
});

var reviewSchema = new mongoose.Schema({
  author: String,
  rating: {type: Number, min: 0, max: 5, required: true},
  createdOn: {type: Date, default: Date.now},
  reviewText: String
});

var locationSchema = new mongoose.Schema({
  name: {type: String, required: true},
  address: {type: String, required: true},
  rating: {type: Number, default: 0, min: 0, max: 5},
  facilities: [String],
  coords: {type: [Number], index: '2dsphere'},
  openingTimes: [openingTimeSchema],
  reviews: [reviewSchema]
});

// compiling schema as 'Location' model
mongoose.model('Location', locationSchema);

In my routes, I map the route to appropriate controller:

router.get('/locations/:locationId', locationsCtrl.locationRead);

In my controller (locationsCtrl.js) I try to find a location by id:

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

module.exports.locationRead = function(req, res) {
  Location
    .findById(req.params.locationId)
    .exec(function(err, location) {
      if (err) throw err;

      res.status(200);
      res.json(location); // returns null
    });
}

When I tested this, I am always getting null for valid ids. Would appreciate some insight as to why. Thanks.

edit:

Checking for the name of the collection on my computer using mongo and show collections, I get locations as the collection name. This is as expected. Although specifying mongoose.model('Location', locationSchema, 'locations') doesn't have any effect.

Answers


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


Need Your Help

CollectionView and UISegmentedControl error

ios objective-c uicollectionview uisegmentedcontrol

I have a little problem in my code, I use a CollectionView with segmentedControl but here when I press section 2 or 3 of segmetedControl and I scroll the CollectionView, I get this error

Is it possible to Develop Apps for multiple Smart TV using any single platform

sdk samsung-smart-tv smart-tv

Is it possible to develop single App for Multiple Smart TV ? I am looking forward to develop an App for Multiple Smart TVs using any single IDE / Platform.