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

460
Views
La consulta Mongoose no se está ejecutando: "cursor.toArray no es una función"

Principiante de MongoDB, tiene problemas para hacer que las consultas funcionen. Estaba siguiendo una especie de tutorial y era una aplicación de notas de demostración. Su sintaxis para guardar nuevas notas funciona bien.

Sin embargo, cuando se trata de imprimir la lista de notas, parece que hay algo mal en la sintaxis que me dieron o algo que estoy haciendo mal.

 const mongoose = require("mongoose"); const url = "mongodb+srv://Saif:<password>@cluster0.8d2lb.mongodb.net/notes-app?retryWrites=true&w=majority"; mongoose.connect(url, { useNewUrlParser: true, }); const noteSchema = new mongoose.Schema({ content: String, date: Date, important: Boolean, }); const Note = mongoose.model("Note", noteSchema); Note.find({}).then((result) => { result.forEach((note) => { console.log(note); }); mongoose.connection.close(); });

Después de buscar documentación, la sintaxis real de find es un poco diferente cuando pasan una devolución de llamada en lugar de usar promesas. Pero cambiar ese bloque para usar una devolución de llamada todavía no funciona

 Note.find({}, (error, data) => { if (error) { console.log(error); } else { data.forEach((note) => { console.log(note); }) } mongoose.connection.close() })

Error

 TypeError: cursor.toArray is not a function at model.Query.<anonymous> (D:\Folders\Documents\CS......... (Use `node --trace-warnings ...` to show where the warning was created) (node:27108) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:27108) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

El método .find del modelo devuelve el objeto de consulta, no el cursor

Para el cursor necesitas hacer .exec

 Note.find({}).exec((error, data) => { if (error) { console.log(error); } else { data.forEach((note) => { console.log(note); }) } mongoose.connection.close() })
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!