Skip to content

I’ve got different behavior for object assign on localhost vs Server

Why the Object.assign works fine on Localhost but on server it does not ?

My vue app is hosted on S3 and everything works fine besides the Object.assign.

The remote api is being called properly and the update is ok, but the object is not being assigned and I got an empty error in the catch. log for console.log(JSON.stringify(e)) is just {}.

      axios
        .put(this.url + "/client/" + item.id, {
          name: item.name,
          contactName: item.contactName,
          phoneNumber: item.phoneNumber,
          email: item.email,
        })
        .then((response) => {
          Object.assign(this.client[this.editedIndex], item);
        })
        .catch((e) => {
          console.log(JSON.stringify(e));
          this.dialogError = true;
        });
    },

I have tried change the object assign like this Object.assign({}, this.client[this.editedIndex], item);, but I got any unexpected behavior.

Answer

The error you get is most likely caused by an empty value of this.client[this.editedIndex]. Take a look on the below example:

(new Promise((resolve) => { resolve(); }))
  .then(() => { 
    console.log('then'); 
    Object.assign(undefined, {a: 1}); 
  })
  .catch((e) => { 
    console.error('error', JSON.stringify(e)) 
  });

prints:

then
error {}

Replacing undefined by null gives similar results. Therefore I would assume that there is no value at your this.client at this.editedIndex key.

In error logging you should avoid using JSON.stringify() with Error instances, as JSON.stringify does not know how to handle it:

> JSON.stringify(new Error('message'))
'{}'

You are losing all the information – like message, stack etc.