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

466
Views
Mongoose query not running - "cursor.toArray is not a function"

MongoDB beginner, having trouble getting queries to work. Was following a tutorial of sorts and it was a demo notes app. Their syntax for saving new notes works fine.

However when it comes to printing out the list of notes, there seems to be something wrong in the syntax given to me or something im doing wrong.

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

After looking up documentation, the actual syntax of find is a little different where they pass in a callback instead of using promises. But changing that block to use a callback still doesnt work

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

.find method from model returns Query object not Cursor

For cursor you need to do .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!