typicode / json-server

Get a full fake REST API with zero coding in less than 30 seconds (seriously)
Other
72.78k stars 7.01k forks source link

Need help to setup routes to access nested collections. #345

Open knmohan opened 8 years ago

knmohan commented 8 years ago

{ "Users": [ { "id": 1, "username": "abc", "password": "xyz" , "userProducts": [{"id": 1, "name": "amzn", "image": "amzn.jpeg" }] } ], "products": [ { "id": 1, "name": "amzn", "image": "img/amzn.jpeg" }, { "id": 2, "name": "bby", "image": "img/bby.jpeg"}, { "id": 3, "name": "gp", "image": "img/gp.jpeg"} ] }

when i try to access the userProducts collection under users using

http://localhost:3000/users/1/userProducts

i get an empty result and 404 errror. Can someone please help me understand how to access the nested collections.

zakuro9715 commented 8 years ago

url is case sensitive. So, Users and users is not same.

You can find userProducts on http://localhost:3000/Users/1/userProducts

knmohan commented 8 years ago

its still not working with the case sensitive approach.

janppires commented 8 years ago

When you access http://localhost:3000/Users/1 you are accessing to a specific record with properties. Your userProducts array is just a property of Users record. I am not expecting to be able to obtain a subset (property) of a record when querying a rest api.

GillesBodart commented 7 years ago

You need to put a UserId property in your "products", like { "id": 1, "name": "amzn", "image": "img/amzn.jpeg", "UserId": 1},

ZenwalkerD commented 5 years ago

url is case sensitive. So, Users and users is not same.

You can find userProducts on http://localhost:3000/Users/1/userProducts

Nope, that doesnt seems to be working for me here. I have a JSON data like:

{ "productName": "Pipe", "productCode": "1-", "productAvailable": true, "productPrice": 122, "productRating": 4, "productDescription": "The Savinelli Tundra Brownblast Briar Pipe series have a very natural almost forest/woodland bushcraft look about them. Savinelli pipes are handmade in Milan Italy, and they are one of the oldest independent brands and one of the largest producers of smoking pipes today.Handcrafted from Briarwood the Savinelli Tundra Brownblast pipes have outstanding beauty. A light sandblasting technique is used to achieve this beautiful bowl then the dark brown stain highlights it's natural character. Finished with a custom made acrylic Cumberland fishtail mouthpiece. One characteristic of Savinelli pipes is the Balsa System filters that clean the smoke without altering the flavour. Of course, the pipes can be used without filter too. The Savinelli Tundra series takes a 9mm Balsa Wood Filter and within the box is a pack of five to get you started.", "productImageURL": "170767/1340130900.svg", "productComments": [ { "id": 0, "dislikes": 0, "likes": 0, "comment": "asd" }, { "id": 1, "dislikes": 0, "likes": 0, "comment": "asdaddsadadasd" } ] }

Having the URL http://localhost:3000/products/1-/productComments doesnt work :(

lallenfrancisl commented 4 years ago

Already answered in issue#72