Reputation: 83
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.
Upvotes: 0
Views: 3541
Reputation: 31
I was facing the same exact error, Using findOne() function should solve this problem
For Example, to find all database items in a blogposts collection;
const mongoose = require('mongoose');
//import BlogPost custom created module
const BlogPost = require('./BlogPost');
mongoose.connect('mongodb://localhost:27017/my_database',{useNewUrlParser:true}).
catch(error => {
console.log(error);
});
BlogPost.findOne({},(error,blogpost)=>{
console.log(error,blogpost);
});
Upvotes: 0
Reputation: 1506
Another potential solution, if you're using MongoDB Atlas, and you're dumb like me 😆:
I was wondering why my dev setup, which had just been working perfectly fine only a few minutes earlier, was suddenly returning the cursor.toArray
error, right when I was trying to demo the setup for a client!
Turns out I had set up a Network Access rule in Atlas that restricted DB access to my local IP address where I was doing development work. When I moved to my client's offices to demo the project, I had also moved to their network and IP Address, without adding a new security rule to my Atlas account for that location. I'm not sure why, but for some reason, MongoDB's response to a lack of security access to the DB server is to return the cursor.toArray
error! I added a new security entry for my current IP, and the problem was resolved.
Seems dumb and obvious in hindsight, but the resultant error was obtuse enough, to warrant me figuring it might be valuable to share!
Moral of the story (TL;DR):
If you're getting the cursor.toArray
error, check your MongoDB Atlas Network Access rules, to see if your current IP address is in the allowed list.
Upvotes: 0
Reputation: 41
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){
....
});
Upvotes: 4
Reputation: 97
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
Upvotes: 3
Reputation: 11
Facing the same error in mongoose version 6 but if run findOne(), works fine
Upvotes: 0
Reputation: 4272
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)=>{});
Upvotes: 0
Reputation: 56
Try installing Version 5.13.8 of mongoose. I had the same problem with 6.0.1
Upvotes: 4