0
Google Analytics에는 두 가지 기능이 있습니다. 다른 함수에서 함수를 호출하려고합니다. 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 (function (x) {'를 details.map (x => {{ –
})로 변경하려고하면 "this"를 화살표 함수에 바인드해야한다. 화살표 함수는 자동으로 "this" – darham
현재 작동 중입니다. 고맙습니다. :) – Parth