I've spent a few hours with copious console.logs trying to work out why this isn't working and am missing something fundamental here:
The scenario is from Simon Holmes' book on Getting MEAN (chapter 6). Apologies for copying the entire function, but I wanted to make sure I wasn't missing something obvious.
I have a schema defined as follows:
var mongoose = require('mongoose');
var reviewSchema = new mongoose.Schema({
author: String,
rating: { type: Number, required: true, min: 0, max: 5 },
reviewText: String,
createdOn: { type: Date, "default": Date.now }
});
var openingTimeSchema = new mongoose.Schema({
days: { type: String, required: true },
opening: String,
closing: String,
closed: { type: Boolean, required: true }
});
var locationSchema = new mongoose.Schema({
name: { type: String, required: true },
address: String,
rating: { type: Number, "default": 0, min: 0, max: 5 },
facilities: [String],
// Always store coordinates longitude, latitude order.
coords: { type: [Number], index: '2dsphere' },
openingTimes: [openingTimeSchema],
reviews: [reviewSchema]
});
mongoose.model('Location', locationSchema);
and some code to read a single location with a specific review:
module.exports.reviewsReadOne = function(req, res) {
console.log('locationid = ' + req.params.locationid);
console.log('reviewid = ' + req.params.reviewid);
if (req.params && req.params.locationid && req.params.reviewid) {
Loc
.findById(req.params.locationid)
.select('name reviews')
.exec (
function(err, location) {
var response, review;
if (!location) {
sendJsonResponse(res, 404, {
"message" : "locationid not found"
});
return;
} else if (err) {
sendJsonResponse(res, 400, err);
return;
}
console.log('reviews = ' + location.reviews);
// Note the rating here...
console.log('#0.rating = ', location.reviews[0].rating);
// Note the id here...
console.log('#0.id = ', location.reviews[0].id);
if (location.reviews && location.reviews.length > 0) {
review = location.reviews.id(req.params.reviewid);
console.log('returned review = ' + review);
if (!review) {
sendJsonResponse(res, 404, {
"message" : "reviewid not found"
});
} else {
response = {
location: {
name : location.name,
id : req.params.locationid
},
review : review
};
sendJsonResponse(res, 200, response);
}
} else {
sendJsonResponse(res, 404, {
"message" : "No reviews found"
});
}
}
);
} else {
sendJsonResponse(res, 404, {
"message" : "Not found, locationid and reviewid are both required"});
}
};
Using Google Postman, I make a query to the API:
localhost:3000/api/locations/5706bbc7b47a0b25981d3a40/reviews/5706bd65b47a0b25981d3a41
But I get a response of
{
"message": "reviewid not found"
}
What's confusing me here is that I believe I'm referencing the correct review subdocument ID, but despire the JSON showing this, Javascript doesn't see it, and might explain why the mongoose function won't return it:
This is my trace:
locationid = 5706bbc7b47a0b25981d3a40
reviewid = 5706bd65b47a0b25981d3a41
reviews = { author: 'Simon Holmes',
id: 5706bd65b47a0b25981d3a41,
rating: 5,
timestamp: Tue Jul 16 2013 00:00:00 GMT+0100 (BST),
reviewText: 'What a great place. I can\'t say enough good things about it.',
createdOn: Fri Apr 08 2016 19:50:15 GMT+0100 (BST) },{ author: 'Jon M',
id: 5706bda2b47a0b25981d3a42,
rating: 5,
timestamp: Tue Sep 09 2014 00:00:00 GMT+0100 (BST),
reviewText: 'Meh...',
createdOn: Fri Apr 08 2016 19:50:15 GMT+0100 (BST) }
#0.rating = 5
#0.id = null
returned review = null
GET /api/locations/5706bbc7b47a0b25981d3a40/reviews/5706bd65b47a0b25981d3a41 404 34.468 ms - 32
As you can see, the location document is found, the reviews subdocuments are found, and it is even possible to printout the rating value of the first item. However, the ID is null despite showing up as having a value in the JSON payload.
Is there something I'm doing wrong here?
As ever, any help appreciated.
Jon