简体   繁体   中英

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.

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 . The document itself is working great in the API but this subdocument I just can't get. The error I get if I provide a reviewid is that it can't find the review 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:

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:"

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:

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:"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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