[英]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.