Open jp-son opened 6 years ago
To access nested object properties, you would access each nested object by its property name, for example:
fetch('http://localhost:8000/results')
.then(res => res.json())
.then(res => console.log(res.values['123'].name))
which outputs"John"
The 123 object was accessed using bracket notation because it is a number and using dot notation would cause an error to throw. Look at the documentation for more info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors
Also, it seems that you're missing commas after each name value, so that could be causing some issues
In OP's example, why doesn't json-server create resources other than for the top level object? For example, why does it not create:
http://localhost/results/values/123
In OP's example, why doesn't json-server create resources other than for the top level object? For example, why does it not create:
http://localhost/results/values/123
Yes, definitely helps to use it as REST API in my app. At the moment, such URL/RAPI doesnt work. Any workaround ?
My JSON looks like this:
{
"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"
}
]
}
via REST API, i cant access productComments object via URL like http://localhost:3000/products/1-/productComments Please help.
In OP's example, why doesn't json-server create resources other than for the top level object? For example, why does it not create:
http://localhost/results/values/123
Yes, this was why I was expecting to be able to do. Then I could've quickly put together some dummy data to simulate a "real" API. Will need to look elsewhere for now, unfortunately.
This issue makes json-server unusable for a lot of API designs. Does anyone have a solution, or perhaps another server that allows this?
you should look at this comment by the author, maybe you'll find it helpful
I wholly agree with a lot of the comments here. The disallowance of accessing properties on an object - say /api/posts/1/whatever - is almost unbelievable. It makes the json-server basically unusable for developing anything like a real product. There are so many obvious situations in which that would be essential. I honestly can't imagine why the developers would restrict it in the way they have.
Yes, we cannot use nested objects but for the purpose of testing we can use custom routes and access that data as seperately With customized prefix --routes routes.json to add custom routes
I wholly agree with a lot of the comments here. The disallowance of accessing properties on an object - say /api/posts/1/whatever - is almost unbelievable. It makes the json-server basically unusable for developing anything like a real product. There are so many obvious situations in which that would be essential. I honestly can't imagine why the developers would restrict it in the way they have.
I agree, either the devs are stupid for intentionally imposing this restriction or they naively wrote garbage code that caused it. For anyone looking for a workaround I've been using a python package I found that supports nesting: https://github.com/gera2ld/json-server.py
Right now, on my json server, all I can access is the results object. But how can I specifically go into the values object, and pick one of its objects. Like how would I access "123" and its properties? Please help me out.