Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

92
Views
Http PUT deletes data that hasn't been changed

I am trying to edit a JSON data base of shifts. I am writing in Javascript using react. This is my understanding of the PUT syntax:

const editShift = async (changed, id) => {

  const res = await fetch(`http://localhost:5000/shifts/${id}`, {
    method: 'PUT', 
    headers: {
    'Content-type': 'application/json' 
    },
    body: JSON.stringify(changed)
 })
 const data = await res.json()
 setShifts([...shifts, data])
}

data.json:

{
  "shifts": [
    {
      "title": "test",
      "startDate": "2018-06-25T07:30:00.000Z",
      "endDate": "2018-06-25T08:00:00.000Z",
      "allDay": false,
      "id": 1
    },
    {
      "title": "test2",
      "startDate": "2018-06-28T07:30:00.000Z",
      "endDate": "2018-06-28T08:00:00.000Z",
      "allDay": false,
      "id": 2
    }
  ]
}

The result is that the new shift will hold only the fields that have been changed and delete the rest. Any ideas why?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

The PUT request should work like that.

A PUT request should PUT the data into place of the old record. You would want to use a PATCH request for only PATCHing data upon the old record.

That set aside, it depends on your /shifts endpoint on how this is actually handled.

about 4 years ago · Juan Pablo Isaza Report

0

To get previous shifts you should use a callback inside setShifts like:

setShifts(prevShifts => ([...prevShifts, data]))
about 4 years ago · Juan Pablo Isaza Report

0

For a question like this, it'd be helpful if you posted before and after examples of data

  • database empty
  • request #1, payload equals X
  • request #2, payload equals Y

Also, knowing what your Server handler is doing would help determine what's going on. I have a feeling what's happening is something like this

// API handler
(req) => {
  const dbData = getDBData();
  dbData.shifts = req.shifts; // overwrite old with new
}

When what you need is something like

(req) => {
  const dbData = getDBData();
  req.shifts.forEach((newShiftData) => {
    const shiftId = newShiftData.id;

    if (dbData[shiftId]) {
      dbData[shiftId] = { ...dbData[shiftId], ...newShiftData }; // merge old with new
    }
  });

  // - check if data changed
  // - update DB
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!