2016-04-08 20 views
2

ben bol console.logs bu çalışmama nedenini yürütmeye çalışıyoruz ile birkaç saat geçirdim ve burada temel bir şey eksik bulunamadı MEAN'DA (bölüm 6). Tüm işlevi kopyalamaktan dolayı özür dilerim, ama belli bir şeyi kaçırmadığımdan emin olmak istedim.Gelincik alt belge kimliği

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); 

ve bazı kod belirli bir inceleme ile tek bir konum okumak için:

ben şöyle bir şema tanımlanmış olan

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"});  
    } 
}; 

tarihinde Postacı kullanarak, API'ına sorgu yapmak :

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

Ama

bir yanıt almak 210
{ 
    "message": "reviewid not found" 
} 

Burada kafa karıştırıcı olan şey, doğru inceleme alt dokümanı kimliğine başvurduğuma inanıyorum, ancak JSON bunu göstererek despire ettiğimi, Javascript'in görmediğini ve mongoose işlevinin neden onu döndürmeyeceğini açıklayabilir :

Bu benim izlemesi: Gördüğünüz gibi

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 

, konum belge bulunursa, yorumlar alt belgeler bulunur ve ilk öğenin derece değerini bir çıktısını almak bile mümkündür. Ancak, ID, JSON yükünde bir değere sahip olarak gösterilmesine rağmen boştur.

Burada yanlış yaptığım bir şey var mı?

Her zamanki gibi, herhangi bir yardım takdir edildi.

Jon

cevap

0

Yani, bu sorun bulundu ve iyiydi kodu ile ilgili değildi. Sorun, kötü bir veri idi. Konum belgesindeki incelemeler alt dokümanı bir '.id' yolu ve bir '_id' yolu içermiyordu.

Benim hatam, ancak kodda neden bir sorun bulamadığımı açık bir şekilde açıklıyor.