【问题标题】:VueJS: Uncaught (in promise) TypeError: Cannot read property 'rol' of undefinedVueJS:未捕获(承诺中)TypeError:无法读取未定义的属性'rol'
【发布时间】:2021-05-14 19:58:13
【问题描述】:

这是一个登录功能 我收到“无法读取未定义的属性 rol”错误:这是我的 vueJs 代码:

methods: {
    send: function() {
      this.error = null;
      this.showLoader = true;
      this.$http
        .post("/login", new FormData(document.getElementById("LoginUser")), {
          reponseType: "json"
        })
        .then(response => 
        {
            localStorage.setItem("role", response.data.data.rol);
            location.href = "/";
          },
          fail => {
            this.showLoader = false;
            this.password = "";
            for (let message of fail.data.data.messages) {
              this.error = this.$t(
                message.message.toLowerCase().replace(/ /g, "_")
              );
            }
          }
        );
    }
  }

我做错了什么?谢谢 我一直在尝试解决这个问题,但我没有成功

【问题讨论】:

  • 这意味着您的response.data 没有属性data,因此您正在尝试访问具有undefined 值的属性rol。也许您不是要添加第二个.data?登录response.data 看看是否符合您的预期。
  • 不,我已经尝试过了,它不是
  • 你可以像这样使用 if(response.data.data){ localStorage.setItem("role", response.data.data.rol); location.href = "/"; }
  • @SebasCarrillo 记录 response.data 显示什么?
  • 您可以在您的问题中添加此回复吗?

标签: javascript vue.js


【解决方案1】:

As per the documentation for vue-resourceresponse 对象没有属性data。您正在寻找的是 response.bodyresponse.json()response.body 解析为 JSON 对象。

【讨论】:

    猜你喜欢
    • 2016-11-22
    • 2019-05-16
    • 2018-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-03
    • 2020-09-24
    • 1970-01-01
    相关资源
    最近更新 更多