我有以下代碼本的NodeJS參考
this.userInfo = 'bla';
request({
url: 'https://api.api.ai/v1/entities?v=20150910',
headers : {
Authorization: "Bearer " + process.env.APIAI_ACCESS_TOKEN
},
method: 'GET'
}, function (error, response, body) {
if (error) {
console.log('Error sending message: ', error);
} else if (response.body.error) {
console.log('Error: ', response.body.error);
}
console.log(this.userInfo);
}.bind(this));
當我嘗試打印this.userInfo
可變我得到undefined
,但我對this
做了bind()
。有人能解釋我發生了什麼事嗎?
似乎應該可以工作良好,你必須重新分配'userInfo' –
亞歷山德羅,這個範圍是失去了在您使用this.userInfo – anomepani