Mongoose - Model.find({}) 不是一个函数。

4

我正在跟随一个Node.js和Mongoose教程,这个教程中我唯一能够通过.collection属性查询集合。所有我看到的教程都说Model.find()Model.findOne()是合法的,但我一直收到相同的错误信息。

我的模型和模式定义在同一文件中。

const fruitSchema = new mongoose.Schema ({
    name: { type: String, required: true },
    rating: { type: Number, required: true },
    review: { type: String, required: true }
});

var collectionName = "fruit";
const f = mongoose.model(collectionName, fruitSchema);

const fruit = new f({
    name: "Apple",
    rating: 7,
    review: "Pretty solid as a fruit."
});

fruit.save();

我已经向上面的fruits集合中添加了5个文档,但是无法直接查询模型:

fruit.Find({}, callback);

图片描述在此处输入

先行致谢。

2个回答

2
你可以在Mongoose的模型上调用find方法。在本例中,f即为该模型。而fruit仅是一个文档,它并没有find方法。
尝试替换为:
var cursor = fruit.find({});

by:

var cursor = f.find({});

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接