gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 22:34:36 25 4
gpt4 key购买 nike

在发送 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com