简体   繁体   English

我正在更新对象,即使变量不知道 express js 中的项目更新

[英]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.

相关问题 即使我使用focus(),列表项也没有获得焦点 - List item is not getting focus even though I am using focus() 即使我没有尝试渲染对象,获取对象也不能作为 React 子错误有效 - Getting a Objects are not valid as a React child error even though I am not trying to render an object AngularJS:即使它们正确显示,为什么也要为此得到NaN? - AngularJS: Why am I getting NaN for this even though they display correctly? 为什么我的本地node.js / express服务器的响应中没有得到JSON对象? - Why am I not getting a JSON object in the response from my local node.js/express server? 即使我可以返回更新后的值,变量也不会改变 - Variable doesn't change even though I can return the updated value 即使我提供数据,为什么我会收到验证错误? - Why am I getting a validation error even though I'm providing data? 即使我的代码中有catch部分,我仍收到未处理的Promise Rejection错误 - I am getting Unhandled Promise Rejection error even though I have the catch section in my code JQuery 未获得更新值,即使 DOM 显示为其他 - JQuery not getting updated value even though DOM shows otherwise 我已经在jQuery中收到函数未定义错误,即使该函数已定义 - I am getting function undefined error in jQuery, even though the function is defined 即使我正在使用非缩小文件,ReactJS也会出现缩小错误 - ReactJS getting minified error even though I am using the non-minified files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM