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
})
}
})
这里ticket
存储在开始时使用sequelize从票据表获取的特定项目
并且在表的if条件中发生了更新。
令我惊讶的是,data: { ...ticket.toJSON(), attachments },
中的ticket
会自动从表中获取更新后的值,尽管它是在更新之前定义的。
这是怎么回事?
1条答案
按热度按时间qlckcl4x1#
变量的行为就像是通过引用而不是值来赋值的。在很多语言中,对象是自动通过引用赋值的。把引用想象成指向真实的对象的快捷方式或指针。基元是通过值赋值的,而不是像你习惯的那样。要通过值赋值对象,你必须复制那个对象。