Home > Net >  JSON file replaces all objects instead of just a specific one
JSON file replaces all objects instead of just a specific one

Time:10-21

I want to be able to just change out one specific object in a JSON file instead of all of them. Right now, I've set up a put request on the front-end using axios which is pushing the data to the back-end where it will be then handled.

However, right now, instead of replacing just the single object which is coming in from the front-end it replaces all of them in the JSON file.

const warehouse = require("../data/warehouses.json");

router.put("/:id", (req, res) => {
    const findData = warehouse.find((info) => info.id === req.params.id);

    fs.writeFile("./data/warehouses.json", JSON.stringify(findData.replace(req.body)), (err) => {
        if (!err) {
            console.log("Data has been changed!");
        }
    });

    res.sendStatus(200);
});

Here's what the file looks like before:

[
  {
    "id": "2922c286-16cd-4d43-ab98-c79f698aeab0",
    "name": "Manhattan",
    "address": "503 Broadway",
    "city": "New York",
    "country": "USA",
    "contact": {
      "name": "Parmin Aujla",
      "position": "Warehouse Manager",
      "phone": " 1 (646) 123-1234",
      "email": "[email protected]"
    }
  },
  {
    "id": "5bf7bd6c-2b16-4129-bddc-9d37ff8539e9",
    "name": "Washington",
    "address": "33 Pearl Street SW",
    "city": "Washington",
    "country": "USA",
    "contact": {
      "name": "Greame Lyon",
      "position": "Warehouse Manager",
      "phone": " 1 (646) 123-1234",
      "email": "[email protected]"
    }
  }
]

and here's after I ran the command:

[
  {
    "id": "2922c286-16cd-4d43-ab98-c79f698aeab0",
    "name": "New Warehouse Data",
    "address": "5th Avenue",
    "city": "Los Angeles",
    "country": "USA",
    "contact": {
      "name": "Parmin Aujla",
      "position": "Warehouse Manager",
      "phone": " 1 (646) 123-1234",
      "email": "[email protected]"
    }
  }
]

enter image description here

You can see that the ID's from the first object match as it's still the same as only the warehouse name and street address were changed.

What part am I missing here, so that it just replaces the object that was changed instead of replacing all of them?

CodePudding user response:

You are storing only the changed element, not replacing it with the new one.

You need to find the index of the changed element and replace it inside the array.

Try this

const warehouse = require("../data/warehouses.json");

  router.put("/:id", (req, res) => {
    const idx = warehouse.findIndex((info) => info.id === req.params.id);

    fs.writeFile("./data/warehouses.json", JSON.stringify(Object.assign([...warehouse], {[idx]: req.body})), (err) => {
        if (!err) {
            console.log("Data has been changed!");
        }
    });

    res.sendStatus(200);
    });

const warehouse = [
  {
    "id": "2922c286-16cd-4d43-ab98-c79f698aeab0",
    "name": "Manhattan",
    "address": "503 Broadway",
    "city": "New York",
    "country": "USA",
    "contact": {
      "name": "Parmin Aujla",
      "position": "Warehouse Manager",
      "phone": " 1 (646) 123-1234",
      "email": "[email protected]"
    }
  },
  {
    "id": "5bf7bd6c-2b16-4129-bddc-9d37ff8539e9",
    "name": "Washington",
    "address": "33 Pearl Street SW",
    "city": "Washington",
    "country": "USA",
    "contact": {
      "name": "Greame Lyon",
      "position": "Warehouse Manager",
      "phone": " 1 (646) 123-1234",
      "email": "[email protected]"
    }
  }
]

let newObj =   {
    "id": "5bf7bd6c-2b16-4129-bddc-9d37ff8539e9",
    "name": "NEW NAME",
    "address": "NEW ADDRESS",
    "city": "NEW CITY",
    "country": "USA",
    "contact": {
      "name": "Greame Lyon",
      "position": "Warehouse Manager",
      "phone": " 1 (646) 123-1234",
      "email": "[email protected]"
    }
  }
  
const idx = warehouse.findIndex((info) => info.id === '5bf7bd6c-2b16-4129-bddc-9d37ff8539e9');
console.log(Object.assign([...warehouse], {[idx]: newObj}))

  • Related