find
статический метод доступен на моделях, в то время как ItemSchema
является схемой.
Это должно быть либо:
ItemSchema.pre('save', async function() {
try{
let count = await Item.find({name : item.name}).count().exec();
....
}catch(err){
throw err;
}
});
const Item = mongoose.model('Item', ItemSchema);
module.exports = Item;
Или:
ItemSchema.pre('save', async function() {
try{
const Item = this.constructor;
let count = await Item.find({name : item.name}).count().exec();
....
}catch(err){
throw err;
}
});
module.exports = mongoose.model('Item', ItemSchema);
Обратите внимание, что Promise.resolve()
является избыточным в функции async
, она уже возвращает разрешенное обещание в случае успеха, как и Promise.reject
.