目前,我正在尝试完成以下内容:
const ItemSchema = mongoose.Schema({
...
name : String
...
});
ItemSchema.pre('save', async function() {
try{
let count = await ItemSchema.find({name : item.name}).count().exec();
....
return Promise.resolve();
}catch(err){
return Promise.reject(err)
}
});
module.exports = mongoose.model('Item', ItemSchema);
但我只得到以下错误:
TypeError: ItemSchema.find不是一个函数。
如何在我的post('save')中间件中调用 .find() 方法? (我知道,模式上有一个唯一的属性。如果它已经存在,我必须这样做来给名字字符串添加后缀)
mongoose版本:5.1.3 nodejs版本:8.1.1 系统:ubuntu 16.04
this.find
替换ItemSchema.find
吗? - kakashi