0
我創建了一個模塊並添加了一些屬性,如果同一月份將更新在同一個用戶ID中,必須顯示錯誤,我沒有任何想法檢查月份是否已插入在loopback nodejs中,保存之前檢查月份是否輸入相同的用戶ID
我創建了一個模塊並添加了一些屬性,如果同一月份將更新在同一個用戶ID中,必須顯示錯誤,我沒有任何想法檢查月份是否已插入在loopback nodejs中,保存之前檢查月份是否輸入相同的用戶ID
我找到答案。重複的條目可以在保存之前使用。
module.exports = function(Payslip) {
var app = require('../../server/server');
Payslip.validatesPresenceOf('userId');
var username_;
var userId;
Payslip.observe('before save', function(ctx, next){
userId = ctx.instance.userId;
app.models.User.findOne({where :{'id': userId}}, function(err, res){
username_ = res.username.replace(/\s/g,'-');
username_ = username_;
var fileName = username_+"-Payslip-"+ctx.instance.month+"-"+ctx.instance.year;
ctx.instance.fileName = fileName;
Payslip.findOne({where: {'fileName': fileName}}, function(err, ctx){
if(ctx){
next('Found duplicate entry');
}
else{
next();
}
})
})
});
};
這很難理解你剛剛提出的問題,但我會盡力幫助你。如果我理解正確,你需要獨特的月/ userId組合?如果是這種情況,那麼你可以在你的模型中使用唯一的索引,你可以包含這兩個字段。如果沒有,請嘗試更好地解釋你的問題,否則人們將無法幫助你。 –
好的。例如,如果我爲一個用戶111添加1月(月),再次爲同一個用戶添加不能發佈的同一月份,則會顯示一些錯誤。你明白嗎? –