skip to Main Content

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);
        }
    })
})

enter image description here

enter image description here

enter image description here

enter image description here

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


  1. 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/splice

    That 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 return true. 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:

    app.route("/user/:id").delete((req, res) => {
        const userId = Number(req.params.id);
        console.log(userId);
    
        // handle case where user ID is not a number
        if(isNaN(userId)) {
            res.sendStatus(400);
            return;
        }
    
        const userIndex = user.findIndex(userRecord => userRecord.id === userId);
    
        // handle case where user does not exist - userIndex will be -1 if the ID was not matched
        if(userIndex < 0) {
            res.sendStatus(404);
            return;
        }
    
        // splice returns an array of deleted elements
        const deletedUsers = user.splice(userIndex, 1);
        console.log("successfully removed=>", deletedUsers);
    
        res.sendStatus(200);
    });
    

    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 the eval() 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 of NaN which stands for Not a Number. You can use the built-in function isNaN() 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.

    const fs = require("fs").promises;
    
    async function writeUsersToFile() {
        const data = JSON.stringify(user);
        await fs.writeFile("./data.json", data);
    }
    

    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 to user.splice().

        // . . .
    
        // splice returns an array of deleted elements
        const deletedUsers = user.splice(userIndex, 1);
        writeUsersToFile();
        console.log("successfully removed=>", deletedUsers);
    
        // . . .
    
    Login or Signup to reply.
  2. 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

    import fs from 'fs';
    let user = require('./data.json');
    
    app.route("/user/:id").delete((req,res)=>{
      const id = Number(req.params.id); // take id to number
      console.log(id);
      const len = user.length; // save len of array before
      user = user.filter(i => i.id !== id); // filter array
      if (len !== user.length) { // if length is different
        // save array to file
        fs.writeFile('./data.json', JSON.stringify(user), (err) => {
          if (err) {
            console.error(err);
          } else {
            console.log('save to file');
          }
        });
      }
    })
    

    P.S.: There is no place to check this code, but it should work

    Login or Signup to reply.
  3. there is some issues in your code need to be corrected

    1. dont use app.route actully you can you app.delete("/user/:id", (req,res)=> {})

    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

    const express = require("express");
    const user = require("./data.json");
    const fs = require("fs");
    const app = express();
    
    
    app.delete("/user/:id", (req, res) => {
      const body = req.params.id
    
      if (isNaN(body)) {
        throw new Error('invalid id parameter');
      }
      let newUsers = users.filter((user) => user.id !== id)
      // or you can use forEach method
    
      fs.writeFile('./data.json', JSON.stringify(newUsers), (error) => {                        
        //fs.unlink to delete 
        if (error) {
          return res.send(error)
        }
    return res.send('successfull')
      })
    })
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search