【发布时间】:2020-08-02 12:22:34
【问题描述】:
我的模型有一个验证器函数,用于检查 ForeignKey。
下面是我的 locationId 属性的模型代码。
locationId: {
type: mongoose.SchemaTypes.ObjectId,
required: true,
validate: {
isAsync: true,
validator: function(v) {
return FKHelper(mongoose.model('Account_LocationMapping'), v);
},
message: `LocationId doesn't exist`
}
}
FK 助手
module.exports = (model, id) => {
return new Promise((resolve, reject) => {
model.findOne({ _id: id }, (err, result) => {
if (result) {
return resolve(true);
} else
return reject(
new Error(`FK Constraint 'checkObjectsExists' for '${id.toString()}' failed`)
);
});
});
};
我的 upsert 查询:
updateOne: {
filter: {
accountId: req.account
},
update: {
$set: { locationId: req.location },
//runValidator:true -> tried setting the validator property inside update.
},
upsert: true,
runValidators: true
}
我尝试设置 runValidator:true 的两种方式都不起作用。
【问题讨论】:
标签: mongodb validation mongoose mongoose-schema