admin管理员组文章数量:1305284
I am creating a database with MongoDB and using the Mongoose ODM. I'm using Node.js. I ran the code without the last block several times and it was fine, but when I wrote the last block in order to use the .find() method, it threw me an odd error.
This is the app.js file:
//require mongoose
const mongoose = require('mongoose');
//connect to mongoDB database
mongoose.connect('mongodb://localhost:27017/albumDB', {useNewUrlParser: true, useUnifiedTopology: true});
//CREATE
//create schema (blueprint/structure)
//of data that we save to the database
const albumSchema = new mongoose.Schema ({
name: String, //the DB has a variable called name with a value of String
author: String,
year: Number,
genre: String,
listened: Boolean,
liked: Boolean
});
//creating the model. parameters: object of collection, schema
const Album = mongoose.model('Album', albumSchema);
//creating the album document
const album = new Album({
name: 'Insurgentes',
author: 'Steven Wilson',
year: 2008,
genre: 'Prog rock',
listened: 1,
liked: 1
});
//save album inside Album inside albumDB
//album.save().then(() => console.log('meow'));
const personSchema = new mongoose.Schema ({
name: String,
age: Number
});
const Person = mongoose.model('Person', personSchema);
const person = new Person({
name: "John",
age: 37
});
//person.save();
const SiameseDream = new Album({
name: 'Siamese Dream',
author: 'The Smashing Pumpkins',
year: 1993,
genre: 'Alt rock, Grunge',
listened: 1,
liked: 1
});
const MellonCollie = new Album({
name: 'Mellon Collie and the Infinite Sadness',
author: 'The Smashing Pumpkins',
year: 1995,
genre: 'Alt rock, Dream pop',
listened: 1,
liked: 1
});
const Adore = new Album({
name: 'Adore',
author: 'The Smashing Pumpkins',
year: 1998,
genre: 'Alt rock, Art rock',
listened: 1,
liked: 1
});
//READ
Album.find(function (err, albums){ //1. error 2.what it finds back
if (err) {
console.log(err);
} else {
console.log(albums);
}
});
This is the error that shows up on my terminal, related to the last block of code:
$ node app.js
TypeError: cursor.toArray is not a function
at model.Query.<anonymous> (C:\Users\user\Desktop\Music\node_modules\mongoose\lib\query.js:2151:19)
at model.Query._wrappedThunk [as _find] (C:\Users\user\Desktop\Music\node_modules\mongoose\lib\helpers\query\wrapThunk.js:27:8)
at C:\Users\user\Desktop\Music\node_modules\kareem\index.js:370:33
at processTicksAndRejections (internal/process/task_queues.js:75:11)
(node:11536) UnhandledPromiseRejectionWarning: MongoInvalidArgumentError: Method "collection.find()" accepts at most two arguments
at Collection.find (C:\Users\user\Desktop\Music\node_modules\mongodb\lib\collection.js:238:19)
at NativeCollection.<puted> [as find] (C:\Users\user\Desktop\Music\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:191:33)
at NativeCollection.Collection.doQueue (C:\Users\user\Desktop\Music\node_modules\mongoose\lib\collection.js:135:23)
at C:\Users\user\Desktop\Music\node_modules\mongoose\lib\collection.js:82:24
at processTicksAndRejections (internal/process/task_queues.js:75:11)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:11536) 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 .html#cli_unhandled_rejections_mode). (rejection id: 1)(node:11536) [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.
I am creating a database with MongoDB and using the Mongoose ODM. I'm using Node.js. I ran the code without the last block several times and it was fine, but when I wrote the last block in order to use the .find() method, it threw me an odd error.
This is the app.js file:
//require mongoose
const mongoose = require('mongoose');
//connect to mongoDB database
mongoose.connect('mongodb://localhost:27017/albumDB', {useNewUrlParser: true, useUnifiedTopology: true});
//CREATE
//create schema (blueprint/structure)
//of data that we save to the database
const albumSchema = new mongoose.Schema ({
name: String, //the DB has a variable called name with a value of String
author: String,
year: Number,
genre: String,
listened: Boolean,
liked: Boolean
});
//creating the model. parameters: object of collection, schema
const Album = mongoose.model('Album', albumSchema);
//creating the album document
const album = new Album({
name: 'Insurgentes',
author: 'Steven Wilson',
year: 2008,
genre: 'Prog rock',
listened: 1,
liked: 1
});
//save album inside Album inside albumDB
//album.save().then(() => console.log('meow'));
const personSchema = new mongoose.Schema ({
name: String,
age: Number
});
const Person = mongoose.model('Person', personSchema);
const person = new Person({
name: "John",
age: 37
});
//person.save();
const SiameseDream = new Album({
name: 'Siamese Dream',
author: 'The Smashing Pumpkins',
year: 1993,
genre: 'Alt rock, Grunge',
listened: 1,
liked: 1
});
const MellonCollie = new Album({
name: 'Mellon Collie and the Infinite Sadness',
author: 'The Smashing Pumpkins',
year: 1995,
genre: 'Alt rock, Dream pop',
listened: 1,
liked: 1
});
const Adore = new Album({
name: 'Adore',
author: 'The Smashing Pumpkins',
year: 1998,
genre: 'Alt rock, Art rock',
listened: 1,
liked: 1
});
//READ
Album.find(function (err, albums){ //1. error 2.what it finds back
if (err) {
console.log(err);
} else {
console.log(albums);
}
});
This is the error that shows up on my terminal, related to the last block of code:
$ node app.js
TypeError: cursor.toArray is not a function
at model.Query.<anonymous> (C:\Users\user\Desktop\Music\node_modules\mongoose\lib\query.js:2151:19)
at model.Query._wrappedThunk [as _find] (C:\Users\user\Desktop\Music\node_modules\mongoose\lib\helpers\query\wrapThunk.js:27:8)
at C:\Users\user\Desktop\Music\node_modules\kareem\index.js:370:33
at processTicksAndRejections (internal/process/task_queues.js:75:11)
(node:11536) UnhandledPromiseRejectionWarning: MongoInvalidArgumentError: Method "collection.find()" accepts at most two arguments
at Collection.find (C:\Users\user\Desktop\Music\node_modules\mongodb\lib\collection.js:238:19)
at NativeCollection.<puted> [as find] (C:\Users\user\Desktop\Music\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:191:33)
at NativeCollection.Collection.doQueue (C:\Users\user\Desktop\Music\node_modules\mongoose\lib\collection.js:135:23)
at C:\Users\user\Desktop\Music\node_modules\mongoose\lib\collection.js:82:24
at processTicksAndRejections (internal/process/task_queues.js:75:11)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:11536) 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/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)(node:11536) [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.
Share
Improve this question
asked Aug 25, 2021 at 20:14
DafDaf
531 silver badge5 bronze badges
3 Answers
Reset to default 5Mongoose has just updated and in 6+ version we have to pass the object as first parameter followed by call back function with params of error and result of query!!
To GET ALL records just pass the empty object.
Album.find({}, function (err, result){ // get all albums
if (err) { // if there will be any error
console.log(err);
} else { /// in success case in which records from DB is fetched
console.log(result);
}
});
https://mongoosejs./docs/api.html#model_Model.find
This issue is facing mongoose version 6.0 So you just have to downgrade the mongoose version. Just run npm uninstall mongoose
to uninstall the current mongoose version then run npm i [email protected]
, this will install the version that will fix your problem. Just check this link https://www.zhishibo./articles/132795.html
Updating to [email protected] fixed the issue for me.
npm run update --save
will update all your dependencies including mongoose
本文标签: javascriptError when trying to use find() in MongooseStack Overflow
版权声明:本文标题:javascript - Error when trying to use .find() in Mongoose - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741796839a2397987.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论