Javascript Mongoose子文档-找不到ID
我花了几个小时研究了大量的console.logs,试图找出这不起作用的原因,我在这里遗漏了一些基本的东西: 这个场景来自西蒙·霍姆斯的《变得卑鄙》(第6章)。很抱歉复制了整个函数,但我想确保没有遗漏任何明显的内容 我有一个模式定义如下:Javascript Mongoose子文档-找不到ID,javascript,api,mongoose,subdocument,Javascript,Api,Mongoose,Subdocument,我花了几个小时研究了大量的console.logs,试图找出这不起作用的原因,我在这里遗漏了一些基本的东西: 这个场景来自西蒙·霍姆斯的《变得卑鄙》(第6章)。很抱歉复制了整个函数,但我想确保没有遗漏任何明显的内容 我有一个模式定义如下: var mongoose = require('mongoose'); var reviewSchema = new mongoose.Schema({ author: String, rating: { type: Number, requ
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却没有看到它,这可能解释了为什么mongoose函数不会返回它:
这是我的追踪:
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
如您所见,找到了location文档,找到了reviews子文档,甚至可以打印出第一项的评级值。但是,尽管在JSON有效负载中显示为具有值,ID仍然为null
我有什么地方做错了吗
一如以往,任何帮助都是值得感激的
Jon因此,我发现了这个问题,它与代码无关,这很好。问题是坏数据。location文档中的reviews子文档具有“.id”路径,而不是“\u id”路径 我的错误,但清楚地解释了为什么我在代码中找不到问题