I am making a function call 3 times with different arguments:
this.getContributorProperties('followers_url', 'contributorFollowers');
this.getContributorProperties('gists_url', 'contributorGists');
this.getContributorProperties('repos_url', 'contributorRepositories');
This function looks like that:
async getContributorProperties(propertyUrl, propertyName) {
const contributors = await this.addLinkToContributor();
for (let i = 0; i < 10; i += 1) {
axios.get(`${contributors[i][propertyUrl]}?per_page=100&${API_KEY}`).then((res) => {
contributors[i][propertyName] = res.data.length;
});
}
return contributors;
}
It loops through an array of contributors (object type) and makes an API call for each one of them. I need to make 3 API calls for each one of them hence the three calls at the beginning. In order to DRY up my code I wanted to make a forEach loop like so:
[
['followers_url', 'contributorFollowers'],
['gists_url', 'contributorGists'],
['repos_url', 'contributorRepositories'],
].forEach(this.getContributorProperties);
forEach loop is in componentDidMount()
When I make 3 calls it works ok. But when I do forEach I get an error:
Uncaught (in promise) TypeError: Cannot read property 'addLinkToContributor' of undefined
How do I make it work?
BONUS: How do I then assign those key-value pairs to each object?