Calling a external functions in nodejs as we do in javascript so that we can re-use the function but in my case its not working. Why?
I am guessing its working asynchronously in nodejs. How do I fix this? Callbacks? I am new to nodejs.
app.get('/googleSyncCallback', passport.authenticate('google', {
failureRedirect: 'url'
}),
function(req, res) {
var contacts = new GoogleContacts({
token: req.token
});
var retrievedContacts = '';
var retrievedContactsArr = [];
contacts.getContacts(function(err, contacts) {
contacts.forEach(function(entry) {
retrievedContacts = {
contact: {
"phone": {
"cell": entry.phoneNumber,
}
},
}
retrievedContactsArr.push(retrievedContacts);
console.log('array... ', retrievedContactsArr[0]); //prints all values
});
});
checkIfContactExists(req, res, retrievedContactsArr);
}
});
function checkIfContactExists(req, res, retrievedContactsArr) {
//PRINTS UNDEFINED..
console.log('array... ', retrievedContactsArr[0]); //// LINE 10
}
printing the array retrievedContactsArr[0]
is returning undefined at line 10.