不能记录猫鼬模型属性



这是我的猫鼬模型的问题。在底部查找代码

粘贴在下面是我使用mongoose与MongoDB交互的node.js代码。由于某些原因,当我运行它时,我得到以下结果:

Starting...
(node:7863) UnhandledPromiseRejectionWarning: MongoInvalidArgumentError: Method "collection.find()" accepts at most two arguments
at Collection.find (/Users/justing/Documents/WebDev/FruitsProject/node_modules/mongodb/lib/collection.js:238:19)
at NativeCollection.<computed> [as find] (/Users/justing/Documents/WebDev/FruitsProject/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:191:33)
at NativeCollection.Collection.doQueue (/Users/justing/Documents/WebDev/FruitsProject/node_modules/mongoose/lib/collection.js:135:23)
at /Users/justing/Documents/WebDev/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:7863) 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:7863) [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.

我应该看到的是Fruit模型实例的name属性。我已经尝试用{}代替参数#1,按照文档,但我似乎不能得到想要的结果,这只是console.log他们。

const mongoose = require("mongoose");
console.log("Starting...");
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);
const fruit = new Fruit ({
name: "Apple",
rating: 7,
review: "Pretty solid as a fruit."
});
fruit.save();
Fruit.find({},function(err,fruits){
if (err) {
console.log(err);
} else {
console.log(fruits);
fruits.forEach(function(fruit){
console.log(fruit.name);
});
}
});

我认为你的代码中没有错误,但是如果这段代码运行多次,你应该在另一个文件中定义你的Fruit Schema,因为你不能在运行时重新定义相同的Schema。

相关内容

  • 没有找到相关文章

最新更新