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

502
Views
No se puede PATCH (.findByIdAndUpdate no es una función)

Estoy tratando de actualizar un documento de base de datos mongoDB para mi lista de tareas pendientes, estoy usando Node, Mongoose, Express y Express Router.

Esquema de mangosta:

 const mongoose = require('mongoose'); const toDoSchema = new mongoose.Schema({ value: { type: String, }, category: { type: String, }, _id: { type: String } }); module.exports = mongoose.model('toDo', toDoSchema)

Código de parche:

 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) } })

Solicitud de insomnio:

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

y lo que obtengo es "updateItem.findByIdAndUpdate no es una función"

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Esta forma está mal. porque no puede usar findByIdAndUpdate con la variable updateItem ... si desea usar findByIdAndUpdate , debe usar el modelo importado y luego usar este método.

código de ejemplo:

 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) } })

Espero que esta solución te ayude.

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