Javascript TypeError:无法读取属性';用户ID';未定义的承诺?
为什么我不能在类中获取属性Javascript TypeError:无法读取属性';用户ID';未定义的承诺?,javascript,Javascript,为什么我不能在类中获取属性userId connection .start() .then(function() { connection .invoke("join", this.userId) .then(function() { this.pushPassportData(); })
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
声明为私有属性类。此
在匿名函数中指的是窗口
,您可以使用箭头函数来解决此问题:
请尝试以下操作:
connection
.start()
.then(()=> {
connection
.invoke("join", this.userId)
.then(()=> {
this.pushPassportData();
})
.catch(err => console.error(err.toString()));
})
.catch(err=> {
return console.error(err.toString());
});