How do I make PATCH request to work when working with internal API?( Express,React,Node.js)

I am trying to set up the backend API using Express. The DELETE,POSTS,GET request all work except for PATCH. I was getting errors before which I have resolved but now I am stumped because there are no errors and I am not sure what to correct at this point.

The request status is 200 instead it GETs the object being edited in its original form.PATCH request in Insomnia

On the server side the code looks like

const express=require('express');
const router=express.Router();
const fs=require('fs'); // file system module
const { Server } = require('http');
const path=require('path');
const { v4: uuidv4 }=require('uuid');
const dataWarehouses=path.join(__dirname, '../data/warehouses.json');

function listWarehouses(){
    const data=fs.readFileSync(dataWarehouses);
    return JSON.parse(data);   
} 
*function patchWarehouse(id,body)*{
    const listWarehouse = listWarehouses();
    const item = NewWarehousesList(body);
    const change = listWarehouse.find((warehouses)=>warehouses.id===id);
    if(change){
        Object.assign(item, change)
        return item
    }
    else{
        ("This Warehouse does not exist")
    }
   fs.writeFileSync(dataWarehouses, JSON.stringify(change));
}

function NewWarehousesList(body){
    return{id: uuidv4(),
        name:body.name,
        address:body.address,
        city:body.city,
        country:body.country,
        contact:[{
            name:body.contact.name,
            position:body.contact.position,
            phone:body.contact.phone,
            email:body.contact.email
        }]
    }
}

router.patch(('/:id'),(req,res)=>{
    res.json(patchWarehouse(req.params.id, req.body ))
})

module.exports=router;



from Recent Questions - Stack Overflow https://ift.tt/36fd91p
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Hibernate Search - Elasticsearch with JSON manipulation

Spring Elasticsearch Operations