在loopback nodejs中,在保存检查之前,使用相同的用户ID输入月份

时间:2017-07-06 10:54:05

标签: node.js loopbackjs

我创建了一个模块并添加了一些属性,如果同月将在同一个用户ID中更新,则必须显示错误,我不知道检查月份是否已插入enter image description here < / p>

1 个答案:

答案 0 :(得分:0)

我发现了答案。重复条目可以在保存之前使用。

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();
            }
        })
    })
 });
};