大量の console.logs を使用して、これが機能しない理由を突き止めようと数時間を費やしましたが、ここで基本的なものが欠落しています。
シナリオは、Getting MEAN に関する Simon Holmes の本 (第 6 章) からのものです。関数全体をコピーして申し訳ありませんが、明らかな何かが欠けていないことを確認したかったのです。
次のように定義されたスキーマがあります。
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);
特定のレビューで単一の場所を読み取るためのコード:
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"});
}
};
Google Postman を使用して、API に対してクエリを実行します。
localhost:3000/api/locations/5706bbc7b47a0b25981d3a40/reviews/5706bd65b47a0b25981d3a41
しかし、私は
{
"message": "reviewid not found"
}
ここで私を混乱させているのは、私が正しいレビュー サブドキュメント ID を参照していると信じているが、これを示す JSON を欲しがっていないことです。Javascript はそれを認識せず、マングース関数がそれを返さない理由を説明している可能性があります。
これは私のトレースです:
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
ご覧のとおり、場所ドキュメントとレビュー サブドキュメントが見つかり、最初のアイテムの評価値を出力することさえ可能です。ただし、JSON ペイロードに値があると表示されているにもかかわらず、ID は null です。
ここで私が間違っていることはありますか?
いつものように、助けていただければ幸いです。
ジョン