mysql - 发送 put 请求时,updatedAt 列未使用最新日期时间更新

标签 mysql express axios sequelize.js

在发送 put 时请求,dailystatus仅列会使用数据进行更新,但 updatedAt列被留下而不更新最新的日期/时间。在 post 过程中,日期和时间自动插入 createdAt 和 updatedAt 列,所以在发送 put req 时,我们是否需要发送指令来更新 updatedAt 列,如果是,我们该怎么做?
//这是为了更新dailystatusupdatedAt可用性表中的列:

`server.js`

    app.put('/service/availability', async (req, res) => {
    
      try {
        const userEmail = req.query.email;
        const dailyStatus =  req.body.dailystatus;
        var selector = {
          where: { email: userEmail }
        };
        var updateData = {dailystatus:dailyStatus};
        const playerDailyStatus = await Availability.update(updateData, selector);
        res.status(200).json({ success: true });
      } catch (e) {
        res.status(500).json({ message: e.message });
      }
    });
Availability.js
    const onUpdate = (dailyinput) =>{
      console.log("Here Daily:"+ dailyinput);
      const dailyStatus = async () => {
          try {
              const params = {
                  email: loginUserEmail,
              };
            const res = await axios.put('http://localhost:8000/service/availability', { dailystatus: dailyinput }, {params} );
            console.log("Dailystatus update:" + res.data.success);
            if (res.data.success) {
              setDeleteDialog(false);
            }
            else {
              console.log(res.data.message);
              setHelperText(res.data.message);
            }
          } catch (e) {
            setHelperText(e.response.data.message);
          }
        }
        dailyStatus();
  }
availability.js模型
'use strict';
module.exports =  (sequelize, DataTypes) => {
    const availability = sequelize.define('availability', {
        email: {
            type: DataTypes.STRING(100),
            allowNull: false

        },
        dailystatus: {
            type: DataTypes.STRING(50),
            allowNull: false
        },
        user_id: {
            type: DataTypes.INTEGER(18)
        },
        id: {
            type: DataTypes.INTEGER(10),
            allowNull: false,
            primaryKey: true,
            autoIncrement: true
        },
        createdAt: {
            type: DataTypes.DATE,
            allowNull: false,
            defaultValue: DataTypes.NOW
        },
        updatedAt: {
            type: DataTypes.DATE,
            allowNull: false,
            defaultValue: DataTypes.NOW
        }
    }, {
        timestamps: true,
        tableName: 'availability'
    });
    availability.associate = function (models) {
        // associations can be defined here
        availability.belongsTo(models.user, {
            foreignKey: 'user_id',
            sourceKey: 'id',
            onDelete: "CASCADE"
        });
    };
    return availability;
}

最佳答案

只需手动添加

var updateData = {
    dailystatus: dailyStatus,
    updatedAt: new Date(),
  };
或者你可以像这样制作钩子(Hook):
'use strict';
module.exports =  (sequelize, DataTypes) => {
    const availability = sequelize.define('availability', {
        email: {
            type: DataTypes.STRING(100),
            allowNull: false

        },
        dailystatus: {
            type: DataTypes.STRING(50),
            allowNull: false
        },
        user_id: {
            type: DataTypes.INTEGER(18)
        },
        id: {
            type: DataTypes.INTEGER(10),
            allowNull: false,
            primaryKey: true,
            autoIncrement: true
        },
        createdAt: {
            type: DataTypes.DATE,
            allowNull: false,
            defaultValue: DataTypes.NOW
        },
        updatedAt: {
            type: DataTypes.DATE,
            allowNull: false,
            defaultValue: DataTypes.NOW
        }
    }, hooks: {
           beforeUpdate: (availability, options) => {
               availability.updatedAt = new Date();
       },
    },{
         timestamps: true,
         tableName: 'availability'
    });
    availability.associate = function (models) {
        // associations can be defined here
        availability.belongsTo(models.user, {
            foreignKey: 'user_id',
            sourceKey: 'id',
            onDelete: "CASCADE"
        });
    };
    return availability;
}

关于mysql - 发送 put 请求时,updatedAt 列未使用最新日期时间更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62629265/

相关文章:

javascript - 如何从 route 获取 Express 中的本地人

api - Electron 作为响应 http 请求的本地 API 服务器?

Python MySQLDB Insert 以变量为参数

mysql - 如果 ID 列是主键,我需要在 mySQL 表上添加索引吗?

sql - 将 MYSQL 查询的 1 列设置为 Null

javascript - axios GET 结果为 "No ' Access-Control-Allow-Origin' header 出现在请求的资源上。”

javascript - 使用 "nested"获取设置钩子(Hook)时,React、Axios、PokeAPI 返回未定义

mysql - 什么会导致 InnoDB 表的单个 UPDATE 性能非常低?

javascript - Puppeteer 未拾取对话框

node.js - 在测试中模拟对外部服务的 axios 请求