【问题标题】:I've got different behavior for object assign on localhost vs Server我在 localhost 与服务器上的对象分配有不同的行为
【发布时间】:2021-01-07 23:02:07
【问题描述】:

为什么 Object.assign 在 Localhost 上可以正常工作,但在服务器上却不行?

我的 vue 应用托管在 S3 上,除了 Object.assign 之外一切正常。

远程 api 被正确调用并且更新正常,但是对象没有被分配并且我在 catch 中得到一个空错误。 console.log(JSON.stringify(e)) 的日志只是 {}。

      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;
        });
    },

我曾尝试像 this Object.assign({}, this.client[this.editedIndex], item); 那样更改对象分配,但出现任何意外行为。

【问题讨论】:

  • 如果您在 catch 日志中看到一个空错误,那么您可以确定 Object.assign 是。也许将 console.log(JSON.stringify(e)) 更改为 console.error(e);得到真正的错误信息。也许 then 函数引发了一个看不见的 javascript 错误,也许是网络请求失败。

标签: javascript vue.js axios


【解决方案1】:

您得到的错误很可能是由空值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 {}

undefined 替换为null 会得到类似的结果。因此,我会假设您的 this.client this.editedIndex 键没有任何价值。

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

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

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

【讨论】:

  • 是的,问题就在这里!感谢@Beniamin H 和 Joshua Angnoe,this.editedIndex 不是正确的索引,所以 this.client 是未定义的。我改变了记录错误的方式,并按照你们告诉我的方式得到了它。非常感谢。
猜你喜欢
  • 2013-11-19
  • 2012-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-21
  • 2014-07-20
  • 2021-03-31
  • 1970-01-01
相关资源
最近更新 更多