2

我花了几个小时处理大量的 console.logs 试图找出为什么这不起作用并且在这里缺少一些基本的东西:

该场景来自 Simon Holmes 的《Getting MEAN》一书(第 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 看不到它,并且可能解释了为什么 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

如您所见,找到了位置文档,找到了评论子文档,甚至可以打印出第一项的评分值。但是,尽管在 JSON 有效负载中显示为具有值,但该 ID 仍为空。

我在这里做错了吗?

一如既往,任何帮助表示赞赏。

乔恩

4

1 回答 1

0

所以,我发现了这个问题,它与代码无关,这很好。问题是错误的数据。位置文档中的评论子文档具有“.id”路径,而不是“_id”路径。

我的错误,但清楚地解释了为什么我在代码中找不到问题。

于 2016-04-09T12:39:16.353 回答