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

嘗試更改'details.map(函數(x){'到'details.map(x => {' –

+0

您必須將「this」綁定到您的箭頭函數,箭頭函數不會自動假設「this」 – darham

+0

它現在正在工作。Thanx很多:) – Parth

回答

2

如果這些功能正在從一個組件中調用,那麼你需要的組件constructorcomponentDidMountbind他們。綁定對於使'this'在回調中起作用是必要的,因爲類方法在React中並未默認綁定。

相關問題