简体   繁体   English

猫鼬和Express子文档API

[英]Mongoose and Express subdocument API

I have an API that works for the documents, but I can't get the subdocuments to work within it. 我有一个适用于文档的API,但无法在其中使用子文档。

Here is the router: 这是路由器:

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

Here is the controller: 这是控制器:

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

The document/subdocument I am trying to get into the API is this . 我试图进入API的文档/子文档是this The document itself is working great in the API but this subdocument I just can't get. 该文档本身在API中运行良好,但是我无法获得此子文档。 The error I get if I provide a reviewid is that it can't find the review ID. 如果我提供了一个reviewid,我得到的错误是它找不到评论ID。

** ------ EDIT ------ ** I was not pushing the subdocument correctly into the document. ** ------编辑------ **我没有将子文档正确地推入文档中。 This is the correct way to push a subdocument with its own ID field into the document: 这是将具有自己的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?",
     }
  }
});

I was simply missing the underscore in "_id:" 我只是在“ _id”中缺少下划线:

I was not pushing the subdocument correctly into the document. 我没有将子文档正确地推入文档中。 This is the correct way to push a subdocument with its own ID field into the document: 这是将具有自己的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?",
     }
  }
});

I was simply missing the underscore in "_id:" 我只是在“ _id”中缺少下划线:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM