Sequelize update更改预先初始化的变量

时间:2016-10-19 03:07:31

标签: node.js sqlite sequelize.js

我试图在更新之前将续集实例保存到变量中。但是在更新之后,旧变量也会更新到新的sequelize实例中,这会使变量的描述变为“旧请求”。不同之前和之后。

var oldRequest = request; // HUGE BUG HERE, OLD REQUEST IS ALSO MODIFIED AS REQUEST IS MODIFIED
        console.log("before " + oldRequest.description);
        attributes.lastUpdater = req.user.get('firstName') + " " + req.user.get('lastName');
        request.update(attributes).then(function(updatedRequest) {
            console.log("after " + oldRequest.description);
            send_update_email(oldRequest ,updatedRequest, req.user.get('email')).then(function() {

1 个答案:

答案 0 :(得分:0)

问题是oldRequest实际上只是引用同一个对象request。请参阅此问题以获得深入解释:Javascript pointer/reference craziness. Can someone explain this?

如果您需要使用克隆来保存description尝试这样的事情:(有关详细信息,请参阅此答案:Cloning an Object in Node.js

var extend = require('util')._extend;
var oldRequest = extend({}, request);
...