sequelize验证多列[this.column未定义]

时间:2019-07-14 12:41:42

标签: javascript node.js express sequelize.js

嗨,我有一些问题,为什么this.day_numberthis.teacher_idundefined

'use strict'

module.exports = (sequelize, DataTypes) => {
    const Teacher = sequelize.models.teachers
    const TimeSlot = sequelize.define('time_slots', {
        day: {
            type: DataTypes.STRING,
            validate: {
                notEmpty: {
                    msg: 'Hari harus diisi.'
                },
                isIn: {
                    args: [['Senin', 'Selasa', 'Rabu', 'Kamis', 'Jumat', 'Sabtu', 'Minggu']],
                    msg: "Hari tidak tersedia."
                }
            }
        },
        day_number: {
            type: DataTypes.TINYINT,
            validate: {
                notEmpty: {
                    msg: 'Urutan hari harus diisi.'
                },
                isInt: {
                    msg: 'Urutan hari harus berupa angka.'
                },
                isIn: {
                    args: [[0, 1, 2, 3, 4, 5, 6]],
                    msg: "Urutan hari tidak tersedia."
                }
            }
        },
        time: {
            type: DataTypes.TIME,
            validate: {
                notEmpty: {
                    msg: 'Waktu mulai harus diisi.'
                },
                isExists: (value, next) => {
                    TimeSlot.findOne({
                        where: { 
                            time: value,
                            day_number: this.day_number,
                            teacher_id: this.teacher_id
                        },
                        attributes: ['id']
                    })
                    .then((data) => {
                        if (data) {
                            return next('Waktu mengajar sudah digunakan.')
                        }
                        next()
                    })
                    .catch((err) => {
                        next(err)
                    })
                }
            }
        },
        teacher_id: {
            type: DataTypes.STRING,
            validate: {
                notEmpty: {
                    msg: 'Guru belum dipilih.'
                },
                isExists: (value, next) => {
                    Teacher.findOne({
                        where: { 
                            id: value 
                        },
                        attributes: ['id']
                    })
                    .then((data) => {
                        if (!data) {
                            return next('Guru tidak tersedia.')
                        }
                        next()
                    })
                    .catch((err) => {
                        next(err)
                    })
                } 
            }
        }
    }, {
        timestamps: true,
        freezeTableName: true,
        updatedAt: 'updated_at',
        createdAt: 'created_at'
    })

    TimeSlot.associate = (models) => {
        TimeSlot.belongsTo(models.teachers, {
            foreignKey: 'teacher_id',
            onDelete: 'CASCADE',
            as: 'teacher'
        })
    }

    return TimeSlot
}

1 个答案:

答案 0 :(得分:0)

您使用的是箭头功能,并且箭头功能不会绑定this。(MDN - Arrow functions

替换所有箭头功能,如下面的代码。

isExists(value, next) {
  TimeSlot.findOne({
      where: { 
          time: value,
          day_number: this.day_number,
          teacher_id: this.teacher_id
      },
      attributes: ['id']
  })
  .then((data) => {
      if (data) {
          return next('Waktu mengajar sudah digunakan.')
      }
      next()
  })
  .catch((err) => {
      next(err)
  })
}