簡體   English   中英

貓鼬和Express子文檔API

[英]Mongoose and Express subdocument API

我有一個適用於文檔的API,但無法在其中使用子文檔。

這是路由器:

router.get('/locations/:locationid/reviews/:reviewid', ctrlReviews.reviewsReadOne);

這是控制器:

module.exports.reviewsReadOne = function(req, res) {
  console.log("getting single review");
// error trap 1: check that locationid exists in request parameters
  if (req.params && req.params.locationid) {
    Loc
      .findById(req.params.locationid)
      .select('name reviews')
      .exec(
        function(err, location) {
          var response, review;
// error trap 2: if mongoose doesn't return a location, send 404 message and exit function scope using return statement
          if (!location) {
            sendJsonResponse(res, 404, {
              "message": "locationid not found"
            });
            return;
// error trap 3: if mongoose returned an error, send it as 404 response and exit controller using return statement
          } else if (err) {
            sendJsonResponse(res, 404, err);
            return;
          }
// check that returned location has reviews
          if (location.reviews && location.reviews.length > 0) {
// use mongoose subdocument .id method as a helper for searching for matching ID
            review = location.reviews.id(req.params.reviewid);
// if review isn't found return an appropriate response
            if (!review) {
              sendJsonResponse(res, 404, {
                "message": "reviewid not found"
              });
// if review is found build response object returning review and location name and ID
            } else {
              response = {
                location: {
                  name: location.name,
                  id: req.params.locationid
                },
                review: review
              };
              sendJsonResponse(res, 200, response);
            }
// if no reviews are found return an appropriate error message
          } else {
            sendJsonResponse(res, 404, {
              "message": "No reviews found"
            });
          }
      });
// if request parameters didn't include locationid, send appropriate 404 response
  } else {
    sendJsonResponse(res, 404, {
      "message": "No reviews found"
    });
  }
};

我試圖進入API的文檔/子文檔是this 該文檔本身在API中運行良好,但是我無法獲得此子文檔。 如果我提供了一個reviewid,我得到的錯誤是它找不到評論ID。

** ------編輯------ **我沒有將子文檔正確地推入文檔中。 這是將具有自己的ID字段的子文檔推入文檔的正確方法:

db.locations.update({
 "name" : "Starcups",
}, {
 $push: {
   'reviews' : {
       author: 'kyle riggen1',
       _id: new ObjectId(),
       rating: 4,
       timestamp: new Date("Jun 1, 2015"),
       reviewText: "will the ID work?",
     }
  }
});

我只是在“ _id”中缺少下划線:

我沒有將子文檔正確地推入文檔中。 這是將具有自己的ID字段的子文檔推入文檔的正確方法:

db.locations.update({
 "name" : "Starcups",
}, {
 $push: {
   'reviews' : {
       author: 'kyle riggen1',
       _id: new ObjectId(),
       rating: 4,
       timestamp: new Date("Jun 1, 2015"),
       reviewText: "will the ID work?",
     }
  }
});

我只是在“ _id”中缺少下划線:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM