JS Fetch CRUD Operations
JS Fetch CRUD Operations
You can modify data in your json-server using JavaScript's fetch API with POST , PUT ,
PATCH , and DELETE requests. Here's how you can do it:
fetch("http://localhost:3000/jobs")
.then(res => res.json())
.then(data => console.log(data))
.catch(err => console.error(err));
js
fetch("http://localhost:3000/jobs", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
title: "Software Engineer",
company: "TechCorp",
salary: 5000
})
})
.then(res => res.json())
1/6
.then(data => console.log("Added:", data))
.catch(err => console.error(err));
js
fetch("http://localhost:3000/jobs/1", {
method: "PUT",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
id: 1, // You must send the `id` again with `PUT`
title: "Senior Software Engineer",
company: "TechCorp",
salary: 7000
})
})
.then(res => res.json())
.then(data => console.log("Updated:", data))
.catch(err => console.error(err));
js
fetch("http://localhost:3000/jobs/1", {
method: "PATCH",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
salary: 8000 // Only updating the salary
2/6
})
})
.then(res => res.json())
.then(data => console.log("Updated:", data))
.catch(err => console.error(err));
js
fetch("http://localhost:3000/jobs/1", {
method: "DELETE"
})
.then(() => console.log("Deleted job with ID 1"))
.catch(err => console.error(err));
TL;DR:
3/6
body: JSON.stringify({
titre: jobTitle,
description: jobDescription,
salaire: jobSalary,
'date de création': jobDate
})})
Yo, my dude, it looks like Vue is tripping over your use of fetch —it's trying to call
_ctx.fetch which ain't defined. 😅 This sometimes happens in Vue when the context gets a
bit messy.
js
4/6
window.fetch(`http://localhost:3000/jobs/${toChange}`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
titre: jobTitle,
description: jobDescription,
salaire: jobSalary,
'date de création': jobDate
})
})
.then(res => res.json())
.then(data => console.log("Updated job:", data))
.catch(err => console.error(err));
3. Component Setup:
If you're using Vue's composition API or similar, verify that you’re not accidentally
binding something to this.fetch in your setup that could override the global fetch.
Try these out, and if it still gives you grief, hit me up! We're here to get that code to work like
a champ. W fr, we got this! 🚀😂
2/2
Yeah, my G, if you're using json-server , it will automatically generate an id for you when
you add a new entry with POST . 🚀
How does it work?
json-server uses a simple in-memory database (or a file like db.json ).
5/6
When you send a POST request without specifying an id , it automatically assigns one
(incrementing from the highest existing ID).
Example:
Sending this:
json
{
"title": "Software Engineer",
"company": "TechCorp",
"salary": 5000
}
json
{
"id": 1,
"title": "Software Engineer",
"company": "TechCorp",
"salary": 5000
}
So you don’t need to manually add an id unless you want to override it.
💡 Important: If you delete all entries and then add a new one, the id might not reset to 1
(it keeps incrementing unless you manually reset the database file).
W json-server, W automation. 🔥😂
6/6