I am trying to create a notification app and here is my code firstly:
function sendNotification(messageUser, messageText, messageDepartment, messageTopic, userIDs) {
var userAndroid = [];
var userIOS = [];
var userDevice;
console.log(userIDs);
for (var i = 0; i < userIDs.length; i++) {
searchRegisterDevices(userIDs[i], function(result) {
if (result) {
for (var j = 0; j < result.length; j++) {
userDevice = {platform: result[j].platform, token: result[j].token};
if (userDevice.platform == 'android') {
userAndroid.push(userDevice);
} else {
userIOS.push(userDevice);
}
}
} else {
console.log("ERROR");
}
});
}
console.log(userAndroid);
if (userAndroid.length > 0) {
}
My issue is, while I am "for looping" gathering my devices, my code continues on to the sending part and fails because userAndroid is empty. What can I do to fix this callback hell as they say? I need to wait until my for loop is finished and then move on to the sending of the message. Any help would be appreciated!