[英]I am getting updated object even though the variable has no knowledge of item update in express js
router.get("/:ticketid", async (req, res) => {
try {
const ticket = await ticketTable.findByPk(req.params.ticketid);
const severityTimeSpaninMinutes = {
"Severity 1 - Critical Business Impact": 1 * 60 * 24,
"Severity 2 - Significant Business Impact": 3 * 60 * 24,
"Severity 3 - Some Business Impact": 90 * 60 * 24,
"Severity 4 - Minimal Business Impact": 91 * 60 * 24
}
if (ticket) {
// check if time until caseSubmittedDate is more than 1 day
const timeUntilCaseSubmittedDate = dayjs(ticket.caseSubmittedDate).diff(dayjs(), 'minute')
// if yes, then update the ticketSLA to "Not Met"
if (timeUntilCaseSubmittedDate < -severityTimeSpaninMinutes[ticket.priority]) {
await ticketTable.update({
ticketSLA: "Not Met"
}, {
where: {
tID: ticket.tID
}
})
}
// get attachments based on R_TID
const attachments = await attachmentTable.findAll({
where: {
R_ID: ticket.tID,
columnName: "Requestor"
}
})
res.json({
data: { ...ticket.toJSON(), attachments },
statusCode: 200,
statusMessage: `Successfully fetched ${ticket.toJSON().ticketNo}`
})
} else {
res.status(400).json({
statusCode: 400,
statusMessage: "Missing Ticket"
})
}
} catch (err) {
return res.status(500).json({
statusCode: 500,
statusMessage: err.message
})
}
})
here ticket
stores particular item fetched from ticket table in the beginning using sequelize这里的ticket
存储在开始时使用 sequelize 从票表中获取的特定项目
And there is an update happening inside an if condition on the table.表上的 if 条件内发生了更新。
To my surprise ticket
in data: {...ticket.toJSON(), attachments },
automatically gets the updated value from the table, even though it was defined before the update.令我惊讶的是数据ticket
data: {...ticket.toJSON(), attachments },
自动从表中获取更新值,即使它是在更新之前定义的。
How this works?这是如何工作的?
The variable is behaving as though it was assigned by reference instead of by value.该变量的行为就好像它是通过引用而不是通过值赋值的。 In a lot of languages objects are automatically assigned by reference.在许多语言中,对象是通过引用自动分配的。 Think of a reference like a shortcut or pointer to the real thing.将引用想象成指向真实事物的快捷方式或指针。 Primitives are assigned by value instead behaving more like what you're used to.基元是按值分配的,而不是表现得更像你习惯的那样。 To assign an object by value, you must copy that object.要按值分配对象,您必须复制该对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.