У меня есть апи с locations, для каждого location - есть список reviews. Написать api ко всем locations получилось, выбрать из списка определенный review зная его id, не вышло. Всё ломается тут
review = location.reviews.id(req.params.reviewid);
, хотя location.reviews =
[ { author: 'Simon Holmes',
id: 57acda33cec290c7a1466cf2,
rating: 5,
timestamp: Thu Jul 16 2015 00:00:00 GMT+0100 (GMT Summer Time),
reviewText: 'What a great place. I can\'t say enough good things about it.',
createdOn: Sun Aug 14 2016 19:21:34 GMT+0100 (GMT Summer Time) },
{ author: 'Simon Holmes',
id: 57acdcb3cec290c7a1466cf7,
rating: 5,
timestamp: Wed Jun 10 2015 00:00:00 GMT+0100 (GMT Summer Time),
reviewText: 'What a great place. I can\'t say enough good things about it.',
createdOn: Sun Aug 14 2016 19:21:34 GMT+0100 (GMT Summer Time) },
{ author: 'Simon Holmes',
id: 57acdcd1cec290c7a1466cf8,
rating: 2,
timestamp: Mon Jun 29 2015 00:00:00 GMT+0100 (GMT Summer Time),
reviewText: 'What a great place. I can\'t say enough good things about it.',
createdOn: Sun Aug 14 2016 19:21:34 GMT+0100 (GMT Summer Time) } ]
а
req.params.reviewid
равняется
57acda33cec290c7a1466cf2
Controller: module.exports.reviewsReadOne = function(req, res) {
console.log("Getting a single review");
if (req.params && req.params.locationid && req.params.reviewid) {
Loc
.findById(req.params.locationid)
.select('name reviews')
.exec(
function(err, location) {
// console.log(location);
var response, review;
if (!location) {
sendJSONresponse(res, 404, {
"message": "locationid not found"
});
return;
} else if (err) {
sendJSONresponse(res, 400, err);
return;
}
console.log(req.params);
console.log(location);
if (location.reviews) {
review = location.reviews.id(req.params.reviewid); // null
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"
});
}
};
Когда делаю запрос через
Postman http://localhost:1337/api/locations/57acd67fcec290c7a1466cf0/reviews/57acda33cec290c7a1466cf2
Получаю в ответ:
{
"message": "reviewid not found"
}
Ответ в Node консоле:
Mongoose connected to mongodb://127.0.0.1:27017/Loc8r
Getting a single review
{ locationid: '57acd67fcec290c7a1466cf0',
reviewid: '57acda33cec290c7a1466cf2' }
{ _id: 57acd67fcec290c7a1466cf0,
name: 'Starcups',
reviews:
[ { author: 'Simon Holmes',
id: 57acda33cec290c7a1466cf2,
rating: 5,
timestamp: Thu Jul 16 2015 00:00:00 GMT+0100 (GMT Summer Time),
reviewText: 'What a great place. I can\'t say enough good things about it.',
createdOn: Sun Aug 14 2016 19:14:38 GMT+0100 (GMT Summer Time) },
{ author: 'Simon Holmes',
id: 57acdcb3cec290c7a1466cf7,
rating: 5,
timestamp: Wed Jun 10 2015 00:00:00 GMT+0100 (GMT Summer Time),
reviewText: 'What a great place. I can\'t say enough good things about it.',
createdOn: Sun Aug 14 2016 19:14:38 GMT+0100 (GMT Summer Time) },
{ author: 'Simon Holmes',
id: 57acdcd1cec290c7a1466cf8,
rating: 2,
timestamp: Mon Jun 29 2015 00:00:00 GMT+0100 (GMT Summer Time),
reviewText: 'What a great place. I can\'t say enough good things about it.',
createdOn: Sun Aug 14 2016 19:14:38 GMT+0100 (GMT Summer Time) } ] }
GET /api/locations/57acd67fcec290c7a1466cf0/reviews/57acda33cec290c7a1466cf2 404 27.935 ms - 32