I'm trying to make an axios call in my Node JS Express app and sending the data to my index.ejs file. The problem is that i can see the data in my callApijavascript file but i cant access the response in my app.js (undefined value).
This is the relevant code from my app.js file:
//iNDEX pAGE RENDER
app.get('/', function (req, rep) {
var response = callApi.GetAll();
var users = response.users;
var activities = response.activities;
console.log('Users ' + users);
console.log('Activities ' + activities);
rep.render('index', {
users: users,
activities: activities
});
});
Here's my callApi.js the logs here return the api data perfectly
var GetAll = function () {
axios.all([
axios.get(initGet.uri + 'users/'),
axios.get(initGet.uri + 'activities/')
])
.then(axios.spread(function (userResponse, activitiesResponse) {
console.log('User', userResponse.data);
console.log('Activities', activitiesResponse.data);
return {
users: userResponse.data,
activities: activitiesResponse.data
};
}));
};
module.exports = {
GetAll: GetAll
};
My Error when i run index.ejs {"error":{"message":"Cannot read property 'users' of undefined"}}
Thanks in advance.