0

I am trying to save a sequelize instance into a variable before update. But after updating, the old variable is also updated into the new sequelize instance, which makes the .description of the variable 'oldRequest' before and after different.

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 Answers1

0

The problem is oldRequest is really just referencing the same object request. See this question for an in depth explaination: Javascript pointer/reference craziness. Can someone explain this?

If you need to save off the description try something like this, using a clone: (See this answer for details: Cloning an Object in Node.js)

var extend = require('util')._extend;
var oldRequest = extend({}, request);
...
Community
  • 1
  • 1
Austin Miller
  • 34
  • 1
  • 4