首页 > 解决方案 > 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.

标签: javascriptvue.jsaxios

解决方案


您得到的错误很可能是由 的空值引起的this.client[this.editedIndex]。看看下面的例子:

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

印刷:

then
error {}

替换undefinednull给出类似的结果。因此,我认为您this.clientthis.editedIndex关键没有任何价值。

在错误记录中,您应该避免将 JSON.stringify() 与Error实例一起使用,因为 JSON.stringify 不知道如何处理它:

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

您正在丢失所有信息 - 例如消息、堆栈等。


推荐阅读