2016-04-08 6 views
2

を見つけていない私は、これは動作していないと、ここで基本的な何かが欠けていますなぜうまくしようとしている豊富なconsole.logsで数時間を費やしてきた:マングースのサブドキュメント - ID

シナリオはサイモンホームズの本からです(第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のポストマンを使用して、私はAPIにクエリを作ります:

localhost:3000/api/locations/5706bbc7b47a0b25981d3a40/reviews/5706bd65b47a0b25981d3a41 

しかし、私は

の応答を取得します210
{ 
    "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 

、ロケーション文書が発見され、レビューがサブ文書が発見され、最初の項目の評価値をプリントアウトすることも可能です。ただし、IDはJSONペイロードに値があるとして表示されていますが、nullです。

私がここで間違っていることはありますか?

これまでどおり、どんな助力にも感謝しています。

jon

答えて

0

私はこの問題を見つけましたが、それは良いコードではありませんでした。問題は悪いデータでした。ロケーションドキュメント内のレビューサブドキュメントには、 '_id'パスではなく '.id'パスがありました。

私の間違いですが、コードで問題が見つからない理由を明確に説明しています。