http request, json-server

hello guys, using http patch how can i modify only certain todo items within the todo array? Example i want to update only the taskName of the 1st array of todo I am using axios Endpoint: http://localhost:3000/tasks/1
{
"title": "title",
"todo": [
{
"taskName": "task 1",
"checked": false
},
{
"taskName": "task 2",
"checked": false
}
],
"id": 1
}
{
"title": "title",
"todo": [
{
"taskName": "task 1",
"checked": false
},
{
"taskName": "task 2",
"checked": false
}
],
"id": 1
}
3 Replies
reddogg476
reddogg4764mo ago
This environment is only accessible to your computer, not accessible on the internet, here. In your javascript, you'll want to try the access modifier property by assigning the JSON string using JSON.parse(jsonStringOfTodos), then you'll want to access it by the array it's included in.
var showtime = JSON.parse(jsonStringOfTodos)
showtime.todo[0].taskName = "New Task Name"
var showtime = JSON.parse(jsonStringOfTodos)
showtime.todo[0].taskName = "New Task Name"
I have working code ofexample in further depth, all in JavaScript (TypeScript) to where code is public in the browser, here: https://www.randomwebbits.com/pages/todos.html The new todo is entered as text and a click event listener takes the string version of it, adding it to the browser's local storage as key-value pairs (json). It's not a perfect match, but it does the same actions under the hood. Open console and click the source file to see/explore the code.
No description
francis
francis4mo ago
Yes i can modify it, my problem is how can i patch that specific todo i modified to the server? Currently i can only patch the whole array and rewrite unmodified array to the server. How can i patch only the modified array to the server? Is it possible?
reddogg476
reddogg4764mo ago
array.splice is a good method for that. You'd take the existing array, splice it, and have a new one. That's what I use with local storage, acting as browser backend. On server-side, if that data is in a database or written to a file or it's in an object file cache, the methods vary.