U
U
ummahusla2016-08-14 21:20:01
MongoDB
ummahusla, 2016-08-14 21:20:01

Problem with REST API (Node, Express, Mongo)?

I have an api with locations, for each location there is a list of reviews. It worked out to write api to all locations, it didn’t work out to select a specific review from the list knowing its id. Everything breaks here

review = location.reviews.id(req.params.reviewid);
even though location.reviews =
[ { author: 'Simon Holmes',
    id: 57acda33cec290c7a1466cf2,
    rating: 5,
    timestamp: Thu Jul 16 2015 00:00:00 GMT+0100 (GMT Summer Time),
    reviewText: 'What a great place. I can\'t say enough good things about it.',
    createdOn: Sun Aug 14 2016 19:21:34 GMT+0100 (GMT Summer Time) },
  { author: 'Simon Holmes',
    id: 57acdcb3cec290c7a1466cf7,
    rating: 5,
    timestamp: Wed Jun 10 2015 00:00:00 GMT+0100 (GMT Summer Time),
    reviewText: 'What a great place. I can\'t say enough good things about it.',
    createdOn: Sun Aug 14 2016 19:21:34 GMT+0100 (GMT Summer Time) },
  { author: 'Simon Holmes',
    id: 57acdcd1cec290c7a1466cf8,
    rating: 2,
    timestamp: Mon Jun 29 2015 00:00:00 GMT+0100 (GMT Summer Time),
    reviewText: 'What a great place. I can\'t say enough good things about it.',
    createdOn: Sun Aug 14 2016 19:21:34 GMT+0100 (GMT Summer Time) } ]

a
req.params.reviewid
equals
57acda33cec290c7a1466cf2

controller:
module.exports.reviewsReadOne = function(req, res) {
    console.log("Getting a single review");
    if (req.params && req.params.locationid && req.params.reviewid) {
        Loc
            .findById(req.params.locationid)
            .select('name reviews')
            .exec(
                function(err, location) {
                    // console.log(location);
                    var response, review;

                    if (!location) {
                        sendJSONresponse(res, 404, {
                            "message": "locationid not found"
                        });
                        return;
                    } else if (err) {
                        sendJSONresponse(res, 400, err);
                        return;
                    }

                    console.log(req.params);
                    console.log(location);

                    if (location.reviews) {
                        review = location.reviews.id(req.params.reviewid); // null
                        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"
        });
    }
};

When I make a request via Postman
http://localhost:1337/api/locations/57acd67fcec290c7a1466cf0/reviews/57acda33cec290c7a1466cf2

I receive in response:
{
  "message": "reviewid not found"
}

Response in Node console:
Mongoose connected to mongodb://127.0.0.1:27017/Loc8r
Getting a single review
{ locationid: '57acd67fcec290c7a1466cf0',
  reviewid: '57acda33cec290c7a1466cf2' }
{ _id: 57acd67fcec290c7a1466cf0,
  name: 'Starcups',
  reviews:
   [ { author: 'Simon Holmes',
       id: 57acda33cec290c7a1466cf2,
       rating: 5,
       timestamp: Thu Jul 16 2015 00:00:00 GMT+0100 (GMT Summer Time),
       reviewText: 'What a great place. I can\'t say enough good things about it.',
       createdOn: Sun Aug 14 2016 19:14:38 GMT+0100 (GMT Summer Time) },
     { author: 'Simon Holmes',
       id: 57acdcb3cec290c7a1466cf7,
       rating: 5,
       timestamp: Wed Jun 10 2015 00:00:00 GMT+0100 (GMT Summer Time),
       reviewText: 'What a great place. I can\'t say enough good things about it.',
       createdOn: Sun Aug 14 2016 19:14:38 GMT+0100 (GMT Summer Time) },
     { author: 'Simon Holmes',
       id: 57acdcd1cec290c7a1466cf8,
       rating: 2,
       timestamp: Mon Jun 29 2015 00:00:00 GMT+0100 (GMT Summer Time),
       reviewText: 'What a great place. I can\'t say enough good things about it.',
       createdOn: Sun Aug 14 2016 19:14:38 GMT+0100 (GMT Summer Time) } ] }
GET /api/locations/57acd67fcec290c7a1466cf0/reviews/57acda33cec290c7a1466cf2 404 27.935 ms - 32

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Kirill Khalitov, 2016-08-17
@Voronar

The answer to our question is here .
And it seems to me clear where we get id from , and not _id .
You after all so added the data through the console?
Quote:
Putting it all together shows something like the following code snippet:

> db.locations.update({ name: 'Starcups'}, {
  $push: {
    reviews: {
      author: 'Simon Holmes',
      id: ObjectId(), //INCORRECT
      rating: 5,
      timestamp: new Date("Jul 16, 2013"),
      reviewText: "What a great place. I can't say enough good things about it."
    }
  }
});

Hence the most likely wrong result.

U
ummahusla, 2016-08-17
@Antonoff

What the author of the book answered:
In the Mongo shell try something like this to add an _id to a review

db.locations.update(
  { "name": "INSERT A LOCATION NAME HERE" },
  {
    $set : {
      "reviews.0._id" : ObjectId()
    }
  }
)

This will add an _id property with an objectId value to the first review subdocument in the location you specify in the command.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question