【发布时间】:2020-02-04 14:57:04
【问题描述】:
AWS lambda 是否支持 mongoose 中间件,我使用.pre() 来检查保存时是否存在数据。
这是我的保存函数调用
res = new cModel();
res.pre('save', function (next) {
cModel.find({name: company.name}, function (err, docs) {
if (!docs.length){
next();
}else{
console.log('company name exists: ',company.name);
next(new Error("Company Name exists!"));
}
});
}) ;
这是我的更新函数调用
company_model.companySchema.pre('update', function (next) {
try {
cModel.find({ name: { $regex: new RegExp(`^${this.getFilter().name}$` , 'i') }}, function (err, docs) {
try {
if (!docs.length) {
next();
} else {
console.log('company name exists: ', this.getFilter().name);
next(new Error("Company Name exists!"));
}
} catch (e) {
console.error('error in cModel.find: ' + e.message);
}
});
} catch (e) {
console.error('error in pre save : ' + e.message)
}
});
【问题讨论】:
-
我认为没有理由不这样做,因为您正确等待所有异步方法完成执行。为什么你会期望它不会呢?你有一些代码来展示这个问题吗?
-
@caffeinated.tech 更新了函数调用
标签: mongodb amazon-web-services mongoose aws-lambda