-
-
Notifications
You must be signed in to change notification settings - Fork 7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Accessing Nested Values #359
Comments
Hi @xseano, There's no way to just "pick" If possible I would recommend this kind of {
"users":[
{
"id":1,
"unm":"sean"
}
],
"todos":[
{
"id":1,
"name":"list1",
"items":[
{
"title":"asd",
"descrp":"asd"
}
],
"userId":1
},
{
"id":2,
"name":"list2",
"items":[
{
"title":"Book",
"descrp":"Read Outliers"
},
{
"title":"sadasd",
"descrp":"Reassasdasdasdssliers"
}
],
"userId":1
}
]
} This way you can do: GET /users/1?_embed=todos # get user 1 + todos
GET /users/1/todos # get all todos for user 1
GET /users/1/todos?name=list1 # get todo for list 1 To create a new todo list, you now have a dedicated URL: POST /users/1/todos # payload: { name: 'list3', items: [] } To update a todo list (all the items will be replaced by the payload, but depending of the size of it, it can be acceptable): PUT /todos/1 # payload { name: 'list1', items: [ { title: "new item" } ] } You may want to have some even finer grained URLs: {
users: []
lists: [
{ id: 1, userId: 1 }
],
items: [
{ id: 1, listId: 1 }
]
} Then you would get:
For inspiration, you can have a look at the doc, Hope it helps. |
WOW, this definitely helps -- thank you so much!!! 👍 |
if i understand correctly this -> POST /users/1/todos # payload: { name: 'list3', items: [] } will update all todos objects in the array no metter what is userId. how can i update specific todo list for specific user? |
posting POST /users/1/todos this will error out and not actually post in /users |
@typicode Saved my day, thanks |
How to access the nested data from json data using json-server. "services": [ |
Hi again Typicode, xD
I was a bit confused about accessing deep properties in your documentation. My question was, how would I access an array within the main object?
For example, here is my db.json:
{ "users": [ { "id": "hhhhadjklsajkdlaskjd", "unm": "sean", "email": "sean.o@mac.com", "pwd": "black", "todos_list1": [ { "id": 4, "title": "asd", "descrp": "asd" } ], "todos_list2": [ { "id": 1, "title": "Book", "descrp": "Read Outliers" }, { "id": 2, "title": "sadasd", "descrp": "Reassasdasdasdssliers" } ], "todos_yo": [
In which I can access the main object under http://localhost:3000/users/hhhhadjklsajkdlaskjd , but since POST with arrays is not supported, how would I be able to access an array like "todos_list1" and view what objects are stored inside this array?
Ex (When I can view todos_list1 specifically, I would see this):
{ "id": 4, "title": "asd", "descrp": "asd" }
Please let me know, or if there is another way to POST objects to an array.
Thanks!
The text was updated successfully, but these errors were encountered: