0
我有兩個不同的谷歌分析功能。我想從另一個函數調用一個函數。我收到一個錯誤,說TypeError: this is undefined
。我不明白爲什麼?如何從另一個函數調用函數?
handleAccounts = (response) => {
var details = response.result.items;
console.log(response);
this.setState({
info: details
});
details.map(function(x) {
gapi.client.analytics.management.webproperties
.list({accountId: x})
.then(this.handleProperties.bind(this)); //This is the line where I am getting the Error.
});
}
handleProperties = (response) => {
// Handles the response from the webproperties list method.
if (response.result.items && response.result.items.length) {
// Get the first Google Analytics account
var firstAccountId = response.result.items[0].accountId;
// Get the first property ID
var firstPropertyId = response.result.items[0].id;
// Query for Views (Profiles).
//queryProfiles(firstAccountId, firstPropertyId);
console.log(response);
} else {
console.log('No properties found for this user.');
}
}
嘗試更改'details.map(函數(x){'到'details.map(x => {' –
您必須將「this」綁定到您的箭頭函數,箭頭函數不會自動假設「this」 – darham
它現在正在工作。Thanx很多:) – Parth