The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

justinc (16) [Avatar] Offline
#1
I am partway through ch6, and have created the controllers and moved the model under the app_api folder.

I have added the code for reviewsReadOne in the reviews.js controller file.
BUT when I send a valid location AND review, it falls through the code to the "no reviews found message"

so via postman
http://localhost:3050/api/locations/598a71f430fa304a8fec0ee3/reviews/598b222130fa304a8fec0ee7

(both are valid entries)

in postman, I see
{
"message" : "no reviews found"
}

here is the console output
{ _id: 598a71f430fa304a8fec0ee3,
name: 'Starcups',
reviews:
[ { reviewText: 'What a great place.',
timestamp: 2017-03-12T05:00:00.000Z,
rating: 5,
_id: 598b1fbf30fa304a8fec0ee4,
author: 'Simon Holmes' },
{ reviewText: 'It was okay. Coffee wasn\'t great, but the wifi was fast.',
timestamp: 2013-06-16T04:00:00.000Z,
rating: 3,
_id: 598b222130fa304a8fec0ee7,
author: 'Charlie Chaplin' } ] }
GET /api/locations/598a71f430fa304a8fec0ee3/reviews/598b222130fa304a8fec0ee7 404 29.314 ms - 30


the location is found, but location.reviews is undefined so this section of the controller code
if (location.reviews && location.reviews.length > 0)
is skipped


in the mongo shell I see this
db.locations.find({"_id":ObjectId("598a71f430fa304a8fec0ee3")}, {name: 1, reviews: 1}).pretty()
{
"_id" : ObjectId("598a71f430fa304a8fec0ee3"),
"name" : "Starcups",
"reviews" : [
{
"author" : "Simon Holmes",
"_id" : ObjectId("598b1fbf30fa304a8fec0ee4"),
"rating" : 5,
"timestamp" : ISODate("2017-03-12T05:00:00Z"),
"reviewText" : "What a great place."
},
{
"author" : "Charlie Chaplin",
"_id" : ObjectId("598b222130fa304a8fec0ee7"),
"rating" : 3,
"timestamp" : ISODate("2013-06-16T04:00:00Z"),
"reviewText" : "It was okay. Coffee wasn't great, but the wifi was fast."
}
]
}

so the _id is correct. What am I missing?
simon.holmes (148) [Avatar] Offline
#2
Hmmm, that seems strange indeed!

Is everything set up in your schema correctly?
justinc (16) [Avatar] Offline
#3
db file
[ 1 KB ]
My schema matches what's on github for the chapter

511357 (2) [Avatar] Offline
#4
schema
[ 876 bytes ]
routes - api
[ 1020 bytes ]
I have a similar problem.
It seems the params review/id never gets matched with the database subdocument review id.
Can't find a revewie by id.
Can't figure out why.

added files / mongodb /

{
"_id": "59be49c6cbfa33df37bedc4a",
"name": "Coffecups",
"address": "Königsstraße 13",
"coords": [
-0.8560884,
57.4545041
],
"reviews": [
{
"author": "Super Maus",
"id": "59be4a08cbfa33df37bedc4b",
"rating": 5,
"timestamp": "2015-07-03T22:00:00.000Z",
"reviewText": "Ich teste die update funktion. ",
"createdOn": "2017-09-24T12:49:44.655Z"
}
],
"openingTimes": [
{
"days": "Monday - Friday",
"opening": "7:00am",
"closing": "7:00pm",
"closed": false
},
{
"days": "Saturday",
"opening": "8:00am",
"closing": "5:00pm",
"closed": false
},
{
"days": "Sunday",
"closed": true
}
],
"facilities": [
"Hot drinks",
"Food",
"Premium wifi"
],
"rating": 4
}
511357 (2) [Avatar] Offline
#5
justinc (16) [Avatar] Offline
#6
I'm not sure if that solution applies.
looking in mongo db and my objects have the _Id and it still doesn't work
here is the return from the mongo client

> db.locations.find({"_id":ObjectId("598a71f430fa304a8fec0ee3")}, {name: 1, reviews: 1}).pretty()
{
"_id" : ObjectId("598a71f430fa304a8fec0ee3"),
"name" : "Starcups",
"reviews" : [
{
"author" : "Simon Holmes",
"_id" : ObjectId("598b1fbf30fa304a8fec0ee4"),
"rating" : 5,
"timestamp" : ISODate("2017-03-12T05:00:00Z"),
"reviewText" : "What a great place."
},
{
"author" : "Charlie Chaplin",
"_id" : ObjectId("598b222130fa304a8fec0ee7"),
"rating" : 3,
"timestamp" : ISODate("2013-06-16T04:00:00Z"),
"reviewText" : "It was okay. Coffee wasn't great, but the wifi was fast."
}
]
}
>