2017-10-11 5 views
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.'); 
    } 
} 
+2

'details.map (function (x) {'를 details.map (x => {{ –

+0

})로 변경하려고하면 "this"를 화살표 함수에 바인드해야한다. 화살표 함수는 자동으로 "this" – darham

+0

현재 작동 중입니다. 고맙습니다. :) – Parth

답변

2

당신은 구성 요소 constructor 또는 componentDidMount 내를 bind해야합니다. 클래스 메소드는 기본적으로 React에서 바인딩되지 않으므로 바인딩은 콜백에서 'this'작업을 수행하는 데 필요합니다.

관련 문제