Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

102
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

0

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

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

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda