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

495
Visualizações
Cannot PATCH (.findByIdAndUpdate is not a function)

I'm trying to update a mongoDB database document for my To-Do list, I'm using Node, Mongoose, Express and Express Router.

Mongoose Schema:

const mongoose = require('mongoose');

const toDoSchema = new mongoose.Schema({

    value: {
        type: String,
    },
    category: {
        type: String,
    },
    _id: {
        type: String
    }
});

module.exports = mongoose.model('toDo', toDoSchema)

Patch code:

router.patch('/changeCategory', async (request, response) =>{
    
    const updateItem = new toDoModel({
        category: request.body.category
    })

    let id = request.body._id
    let category = request.body.category
    
    try{
        await updateItem.findByIdAndUpdate({_id: id},{category: category})
        response.json('Category changed')
    }
    catch (error){
        console.log(error.message)
    }
})

Insomnia request:

PATCH http://localhost:5000/changeCategory
{
    "category":"inProgress",
    "_id": "628de6210da8e45f52217083"
}

and what i get is "updateItem.findByIdAndUpdate is not a function"

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

This way is wrong. because you can't use findByIdAndUpdate with updateItem variable ..if you want use findByIdAndUpdate, you should use the imported model, then use this method.

example code:

const toDoModel = require("../models/your-model")

router.patch('/changeCategory', async (request, response) => {
  let category = request.body.category
  const updateItem = new toDoModel({
    category
  })

  let id = request.body._id

  try {
    // This part was changed *****

    await toDoModel.findByIdAndUpdate(id,{category: category})
    response.json('Category changed')

    // *******
  } catch (error) {
    console.log(error.message)
  }
})

I hope this solution helps you.

over 4 years ago · Santiago Trujillo 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