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

96
Visualizações
PUT request crashes the server

I'm getting this error when trying to update an entry (in this case, checking a todo and setting it to true/false) It seems like i'm getting the proper object (seen in the data console log) which makes me wonder why i'm getting [object Object].

Would appreciate any kind of help!

enter image description here

Here's the action :

export function onSaveTodo(todo) {
  return async (dispatch) => {
    try {
      const savedTodo = await todosService.saveTodo(todo);

      const action = {
        type: 'UPDATE_TODO',
        todo: savedTodo,
      };

      dispatch(action);
    } catch (err) {
      console.log('cant save todo');
    }
  };
}

the service :

const BASE_URL = 'todo';

async function saveTodo(todo) {
  try {
    if (todo._id) {
      return httpService.put(BASE_URL, todo);
    } else {
      return httpService.post(BASE_URL, todo);
    }
  } catch (err) {
    console.log(`could not save todo `, err);
    throw err;
  }
}

Http service :

export const httpService = {
  get(endpoint, data) {
    return ajax(endpoint, 'GET', data);
  },
  post(endpoint, data) {
    return ajax(endpoint, 'POST', data);
  },
  put(endpoint, data) {
    console.log('endpoint:', endpoint);
    console.log('data:', data);

    return ajax(endpoint, 'PUT', data);
  },
  delete(endpoint, data) {
    return ajax(endpoint, 'DELETE', data);
  },
};

The backend controller :

async function updateTodo(req, res) {
  try {
    const { todo } = req.body;
    // console.log('todo:', todo);

    const savedTodo = await todoService.update(todo);

    res.json(savedTodo);
  } catch (err) {
    logger.error('Failed to update todo', err);
    res.status(500).send({ err: 'Failed to update todo' });
  }
}

and the backend service :


async function update(todo) {
  try {
    const newTodo = {
      ...todo,
      _id: ObjectId(todo._id),
    };

    console.log('newTodo:', newTodo);

    const collection = await dbService.getCollection('todo');
    await collection.updateOne({ _id: newTodo._id }, { $set: newTodo });
    return todo;
  } catch (err) {
    logger.error(`Can not update toy ${todo._id}`, err);
    throw err;
  }
}
about 4 years ago · Juan Pablo Isaza
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