我花了几个小时与丰富的console.logs一起试图解决为什么这不起作用并且遗漏了一些基本的东西:
情景来自Simon Holmes'关于获得意义的书(第6章)。复制整个功能的道歉,但我想确保我没有遗漏任何明显的东西。
我的架构定义如下:
<h1 class="GreenLrg" align="center">Related Activities</h1>
<div align="center"> <a href="/activities/kauai/ziplineadventures/koloa-zipline.htm">
<div class="CatBox"><img src="/Portals/0/1koloa-zipline-tour-2.jpg" height="174" width="231"><span>Koloa Zipline Tour</span></div>
</a> <a href="/activities/kauai/ziplineadventures/zip-n-dip-expedition.htm">
<div class="CatBox"><img src="/Portals/0/2zip-n-dip-2.jpg" height="174" width="231"><span>Zip N' Dip Expedition</span></div>
</a> <a href="/activities/kauai/ziplineadventures/kipu-falls-safari.htm">
<div class="CatBox"><img src="/Portals/0/3kipu-zipline-safari-2.jpg" height="174" width="231"><span>Kipu Zipline Safari</span></div>
<p></p>
</a></div><a href="/activities/kauai/ziplineadventures/kipu-falls-safari.htm">
以及使用特定评论阅读单个位置的一些代码:
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);
使用Google Postman,我对API进行了查询:
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"});
}
};
但我收到了
的回复localhost:3000/api/locations/5706bbc7b47a0b25981d3a40/reviews/5706bd65b47a0b25981d3a41
让我感到困惑的是,我相信我引用了正确的评论子文档ID,但是让JSON显示出来,Javascript没有看到它,并且可能解释为什么猫鼬功能不会回来的:
这是我的追踪:
{
"message": "reviewid not found"
}
如您所见,找到了位置文档,找到了评论子文档,甚至可以打印出第一个项目的评级值。但是,尽管ID显示为在JSON有效负载中具有值,但ID仍为空。
我在这里做错了吗?
与以往一样,任何帮助都表示赞赏。
乔恩
答案 0 :(得分:0)
所以,我发现了这个问题,它与代码无关,这很好。问题是糟糕的数据。位置文档中的评论子文档具有“.id”路径,而不是“_id”路径。
我的错误,但清楚地解释了为什么我在代码中找不到问题。