簡體   English   中英

TypeError:無法讀取 promise 中未定義的屬性“userId”?

[英]TypeError: Cannot read property 'userId' of undefined in promise?

為什么我無法在 class 中獲取屬性userId

   connection
          .start()
          .then(function() {
            connection
              .invoke("join", this.userId)
              .then(function() {
                this.pushPassportData();
              })
              .catch(err => console.error(err.toString()));
          })
          .catch(function(err) {
            return console.error(err.toString());
          });

在這一行:

 .invoke("join", this.userId)

userId被聲明為私有屬性 class。

this在你的匿名 function 中是指window ,你可以使用箭頭函數來解決這個問題:

嘗試以下操作:

connection
          .start()
          .then(()=> {
            connection
              .invoke("join", this.userId)
              .then(()=> {
                this.pushPassportData();
              })
              .catch(err => console.error(err.toString()));
          })
          .catch(err=> {
            return console.error(err.toString());
          });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM