I am trying to add to an object in Node:
exports.getAthleteData = function(accessToken, athleteId) {
var athleteData = {};
strava.athlete.get({'access_token': accessToken},function(err, payload) {
athleteData.push({
athleteName: payload.firstname + ' ' + payload.lastname,
profile: payload.profile,
profileMedium: payload.profile_medium,
isPremium: payload.premium,
sex: payload.sex
});
});
return athleteData;
}
When calling the function I get an error TypeError: athleteData.push is not a function.
If I remove .push and do the below, all that is returned is {}.
athleteData = {
athleteName: payload.firstname + ' ' + payload.lastname,
profile: payload.profile,
...
To be sure, I dumped payload in the console and it gave me the JSON object I was expecting.
I'm certainly missing something very simple. This is my first foray into Node/Express and I'm really enjoying it, but I need to improve my JavaScript after years of relying on jQuery.
athleteDatais an object. Objects don't have apushmethod - did you mean to define it as an array?var athleteData = []- I also worry thatstrava.athlete.getis an async method and you're returning before the call is complete.athleteDatais an object not an array ,it doesn't have apush()method.athleteDatalike that.strava.athlete.getis async, so you have to do a callback withathleteData.