I am trying to create API and for client purpose i am using node postman.In delete method client is able to delete the json object but it is not reflecting in json file.
const express = require("express");
const user = require("./data.json");
const fs = require("fs");
const app = express();
app.route("/user/:id").delete((req,res)=>{
const body = req.params.id;
console.log(eval(body));
user.map((obj)=>{
if(obj.id==eval(body)){
console.log("matched");
console.log(user.indexOf(obj));
delete user[user.indexOf(obj)];
console.log("successfully removed=>"+obj);
}
})
})
JSON file data:
[
{
"id": 1,
"first_name": "Lilah",
"last_name": "Smitherham",
"email": "[email protected]",
"gender": "Female",
"title": "Accountant I"
},
{
"id": 2,
"first_name": "Donaugh",
"last_name": "Zanni",
"email": "[email protected]",
"gender": "Male",
"title": "Quality Control Specialist"
},
{
"id": 3,
"first_name": "Neila",
"last_name": "Hillyatt",
"email": "[email protected]",
"gender": "Female",
"title": "Registered Nurse"
},
{
"id": 4,
"first_name": "Granny",
"last_name": "Baszkiewicz",
"email": "[email protected]",
"gender": "Male",
"title": "Teacher"
},
{
"id": 5,
"first_name": "Reinaldos",
"last_name": "Christophe",
"email": "[email protected]",
"gender": "Male",
"title": "Recruiter"
},
{
"id": 6,
"first_name": "Zebedee",
"last_name": "Bulford",
"email": "[email protected]",
"gender": "Male",
"title": "Programmer Analyst II"
},
{
"id": 7,
"first_name": "Maxie",
"last_name": "Rudinger",
"email": "[email protected]",
"gender": "Female",
"title": "Geologist I"
},
{
"id": 8,
"first_name": "Vivi",
"last_name": "Hiscocks",
"email": "[email protected]",
"gender": "Female",
"title": "Software Consultant"
},
{
"id": 9,
"first_name": "Adlai",
"last_name": "Charer",
"email": "[email protected]",
"gender": "Male",
"title": "Senior Cost Accountant"
},
{
"id": 10,
"first_name": "Loydie",
"last_name": "Stribling",
"email": "[email protected]",
"gender": "Male",
"title": "VP Product Management"
},
{
"id": 11,
"first_name": "Farly",
"last_name": "Champness",
"email": "[email protected]",
"gender": "Male",
"title": "Operator"
},
{
"id": 12,
"first_name": "Jasper",
"last_name": "Dollimore",
"email": "[email protected]",
"gender": "Male",
"title": "Media Manager IV"
}
//so on 1000 users.
]
where is the mistake?
3
Answers
Since you’ve loaded your JSON data into an in-memory array, you can use various Array functions on it to search and modify it. When you’re wanting to remove a specific item from it, you can use the
Array.splice()
function which you can read more about here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/spliceThat function takes the element index that you want to remove, and then the number of elements that you want to remove from that position. In this case you just want to remove one.
You can find the index of that user by making use of the
Array.findIndex()
function. This takes a callback which matches against each record until you find the one you want and you returntrue
. It then knows the index of the record and passes it back to you for you to use.I would suggest using the following code for your DELETE endpoint:
I already mentioned this in the replies, but I removed the calls to
eval()
as it would pose a serious security risk in your application. Since a user can write an arbitrary string into that part of the URL, they could potentially pass arbitrary JavaScript code into your application which then gets run by theeval()
function. This means an attacker could run their own code on your server, leading to it being compromised.Since all you needed to do was convert the string to a number, you can make use of the
Number()
constructor function which is a handy way of doing this conversion. It also doesn’t crash if the string you passed in wasn’t a real number, and instead just gives you a special JavaScript value ofNaN
which stands for Not a Number. You can use the built-in functionisNaN()
to quickly check that before proceeding.One more thing to keep in mind though is that this will only edit the list of users in memory, and it won’t edit the JSON file. If you would like these changes to persist in your JSON file, you will have to write the JSON back out to the file each time you update or delete a user.
You can then call the
writeUsersToFile();
function from your DELETE endpoint and any other place where you modify the user list. In this case, you’d put this call underneath the call touser.splice()
.You get an ID to delete.
It is necessary to filter your current array by ID, excluding the passed ID from it and save it to a file
P.S.: There is no place to check this code, but it should work
there is some issues in your code need to be corrected
2.dont use eval beacuse its have security risks and attacker can run scripts in your code Eval function
3.you using delete method in an array which just set that properties to undefind
4.you dont defined any case that return the response back to the user which may cuase the request hanging