gpt4 book ai didi

javascript - 尝试在 Mongoose 中使用 .find() 时出错

转载 作者:行者123 更新时间:2023-12-05 01:06:33 25 4
gpt4 key购买 nike

我正在使用 MongoDB 创建数据库并使用 Mongoose ODM。我正在使用 Node.js。我在没有最后一个 block 的情况下运行了几次代码,这很好,但是当我编写最后一个 block 以使用 .find() 方法时,它给了我一个奇怪的错误。

这是 app.js 文件:

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

这是在我的终端上显示的错误,与最后一段代码有关:

$ 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.<computed> [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.org/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.

最佳答案

Mongoose 刚刚更新到 6+ 版本我们必须将对象作为第一个参数传递,然后是带有错误参数和查询结果的回调函数!

要获取所有记录,只需传递空对象。

    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.com/docs/api.html#model_Model.find

关于javascript - 尝试在 Mongoose 中使用 .find() 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68929315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com