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.<computed> [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.org/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.
Try installing Version 5.13.8 of mongoose. I had the same problem with 6.0.1