序列化验证不适用于PUT方法

时间:2019-10-30 15:14:03

标签: javascript sqlite express sequelize.js

我对Sequelize很陌生。这是我的模特。验证似乎可以在POST方法上正常工作,但不能在PUT方法上工作。我不确定我在这里缺少什么。我将非常感谢所有帮助人员。

const Sequelize = require('sequelize');

module.exports = (sequelize) => {
  class Course extends Sequelize.Model {}
  Course.init({
    id: {
      type: Sequelize.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    title: {
      type: Sequelize.STRING,
      allowNull: false,
      validate: {
        notNull: {
          msg: 'Please provide a value for "title"'
        },
        notEmpty: {
          msg: 'Please provide a value for "title"'
        }
      }
    },
    description: {
      type: Sequelize.TEXT,
      allowNull: false,
      validate: {
        notNull: {
          msg: 'Please provide a value for "description"'
        },
        notEmpty: {
          msg: 'Please provide a value for "description"'
        }
      }
    }
  }, { sequelize });

  Course.associate = (models) => {
    Course.belongsTo(models.User, {
      foreignKey: {
       fieldName: 'userId',
       allowNull: false
      }
    })
  };

  return Course
}

这是我在主要路线上的代码。

router.put('/courses/:id', asyncHandler(async(req, res) => {
  try {
    const course = await Course.findByPk(req.params.id)
    await course.update(req.body);
    res.status(204).end();
  } catch (error) {
    console.error(error);
  }
}));

0 个答案:

没有答案