Answer the question
In order to leave comments, you need to log in
How to add a new element (object) to an array which is in an object using fetch/post?
Hello, to the point:
I am creating a ToDo web application, a project for myself, so to speak, for study. While working with fetch / post, a question arose for which I could not find a normal answer, and I decided to ask it here.
I have a db.json file which is located on a local server ( json-server )
like this:
{
"usersTodos": [
{
"id": 1000,
"todos": []
}
]
}
fetch('http://192.100.0.0:3000/usersTodos', {
method: 'post',
headers: {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json'
},
body: JSON.stringify(obj)
})
Answer the question
In order to leave comments, you need to log in
because typicode /json-server
is used
, you can apparently change the saved data by their id, with a PATCH request:
const obj = {
todos: [
{title: "QnA", description: "прочитать документацию"},
],
}
fetch('http://192.100.0.0:3000/usersTodos/1000', { // здесь 1000 это id
method: 'patch',
headers: {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json'
},
body: JSON.stringify(obj)
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question