admin管理员组

文章数量:1318573

I am new to node js and currently i am practicing mongoDB via mongoose 6.0. Here i am getting this error on find method while trying to search the database:

There was an error
TypeError: cursor.toArray is not a function
    at model.Query.<anonymous> (C:\Users\Ishan\Desktop\Web Dev\Backend lul\FruitsProject\node_modules\mongoose\lib\query.js:2151:19)
    at model.Query._wrappedThunk [as _find] (C:\Users\Ishan\Desktop\Web Dev\Backend lul\FruitsProject\node_modules\mongoose\lib\helpers\query\wrapThunk.js:27:8)
    at C:\Users\Ishan\Desktop\Web Dev\Backend lul\FruitsProject\node_modules\kareem\index.js:370:33
    at processTicksAndRejections (internal/process/task_queues.js:77:11)
(node:13720) UnhandledPromiseRejectionWarning: MongoInvalidArgumentError: Method "collection.find()" accepts at most two arguments
    at Collection.find (C:\Users\Ishan\Desktop\Web Dev\Backend lul\FruitsProject\node_modules\mongoose\node_modules\mongodb\lib\collection.js:238:19)
    at NativeCollection.<puted> [as find] (C:\Users\Ishan\Desktop\Web Dev\Backend lul\FruitsProject\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:191:33)
    at NativeCollection.Collection.doQueue (C:\Users\Ishan\Desktop\Web Dev\Backend lul\FruitsProject\node_modules\mongoose\lib\collection.js:135:23)
    at C:\Users\Ishan\Desktop\Web Dev\Backend lul\FruitsProject\node_modules\mongoose\lib\collection.js:82:24
    at processTicksAndRejections (internal/process/task_queues.js:77:11)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:13720) 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 `--u
terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see .html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:13720) [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.

while trying to read from the database using collection.find() method.

const mongoose = require('mongoose');

mongoose.connect("mongodb://localhost:27017/fruitsDB", {useNewUrlParser: true})

const fruitSchema = new mongoose.Schema ({
  name: String,
  rating: Number,
  review: String
});

const Fruit = mongoose.model("Fruit", fruitSchema); //creates collection

const fruit= new Fruit({
  name: "Apple",
  rating: 7,
  review: "good"
});

const kiwi= new Fruit({
  name: "kiwi",
  rating: 7,
  review: "good"
});

const banana= new Fruit({
  name: "banana",
  rating: 7,
  review: "good"
});

// Fruit.insertMany([kiwi,banana],function(err){
//   if (err){
//     console.log(err);
//   }
//   else{
//     console.log("Successfully inserted");
//   }
// })
//

Fruit.find(function(err, fruits){ //shows error right here
  if(err){
    console.log("There was an error");
    console.log(err);
  }else{
    console.log(fruits);
  }
}); 


i even tried downgrading the mongoose version but it still showed the same error. The insertion works fine as i practiced it first and later on added that "find" method.

I am new to node js and currently i am practicing mongoDB via mongoose 6.0. Here i am getting this error on find method while trying to search the database:

There was an error
TypeError: cursor.toArray is not a function
    at model.Query.<anonymous> (C:\Users\Ishan\Desktop\Web Dev\Backend lul\FruitsProject\node_modules\mongoose\lib\query.js:2151:19)
    at model.Query._wrappedThunk [as _find] (C:\Users\Ishan\Desktop\Web Dev\Backend lul\FruitsProject\node_modules\mongoose\lib\helpers\query\wrapThunk.js:27:8)
    at C:\Users\Ishan\Desktop\Web Dev\Backend lul\FruitsProject\node_modules\kareem\index.js:370:33
    at processTicksAndRejections (internal/process/task_queues.js:77:11)
(node:13720) UnhandledPromiseRejectionWarning: MongoInvalidArgumentError: Method "collection.find()" accepts at most two arguments
    at Collection.find (C:\Users\Ishan\Desktop\Web Dev\Backend lul\FruitsProject\node_modules\mongoose\node_modules\mongodb\lib\collection.js:238:19)
    at NativeCollection.<puted> [as find] (C:\Users\Ishan\Desktop\Web Dev\Backend lul\FruitsProject\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:191:33)
    at NativeCollection.Collection.doQueue (C:\Users\Ishan\Desktop\Web Dev\Backend lul\FruitsProject\node_modules\mongoose\lib\collection.js:135:23)
    at C:\Users\Ishan\Desktop\Web Dev\Backend lul\FruitsProject\node_modules\mongoose\lib\collection.js:82:24
    at processTicksAndRejections (internal/process/task_queues.js:77:11)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:13720) 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 `--u
terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:13720) [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.

while trying to read from the database using collection.find() method.

const mongoose = require('mongoose');

mongoose.connect("mongodb://localhost:27017/fruitsDB", {useNewUrlParser: true})

const fruitSchema = new mongoose.Schema ({
  name: String,
  rating: Number,
  review: String
});

const Fruit = mongoose.model("Fruit", fruitSchema); //creates collection

const fruit= new Fruit({
  name: "Apple",
  rating: 7,
  review: "good"
});

const kiwi= new Fruit({
  name: "kiwi",
  rating: 7,
  review: "good"
});

const banana= new Fruit({
  name: "banana",
  rating: 7,
  review: "good"
});

// Fruit.insertMany([kiwi,banana],function(err){
//   if (err){
//     console.log(err);
//   }
//   else{
//     console.log("Successfully inserted");
//   }
// })
//

Fruit.find(function(err, fruits){ //shows error right here
  if(err){
    console.log("There was an error");
    console.log(err);
  }else{
    console.log(fruits);
  }
}); 


i even tried downgrading the mongoose version but it still showed the same error. The insertion works fine as i practiced it first and later on added that "find" method.

Share Improve this question edited Aug 26, 2021 at 5:29 Something Nice asked Aug 25, 2021 at 16:44 Something NiceSomething Nice 831 silver badge8 bronze badges
Add a ment  | 

8 Answers 8

Reset to default 4

Try installing Version 5.13.8 of mongoose. I had the same problem with 6.0.1

The current version of mongoose will raise the error mentioned if you follow the steps laid out for Model.find(). No issues for Model.findOne() though.

var apple = Fruit.find( { } , { prop1: 1, prop2: 1, _id: 1 } )
    .exec(function(err, fruitsArray){
        ....
    });

I had the same error yesterday and installing the version 5.13.8 fixed it with no errors.

Just run npm uninstall mongoose <= to uninstall the current mongoose version 6.

Then run npm i [email protected] <= this will install the version that will fix your problem

Most likely it is bcuz you have windows and that’s why it cashes, for the undefined parameter find({}); even if you don’t specify nothing in the object it has to be there

collection.find({}, (er, te)=>{});

Facing the same error in mongoose version 6 but if run findOne(), works fine

Another potential solution, if you're using MongoDB Atlas, and you're dumb like me

本文标签: