As i am new to NodeJS, I have tried several methods such as using async/await on my functions but when i try to print out the array retrieveValues it always seems to be empty.
Even trying await doesn't help it, if i print out the retrieveValues at the point that it pushes into the array, it will show in the console but not when i do it after.
router.post('/api/retrievefornotifications', (request, response) => {
var requestBody = request.body;
var responseCode;
var teacher = requestBody.teacher;
var notification = requestBody.notification;
var emails = helper.findEmailAddresses(notification);
console.log(emails);
var retrieveValues = {
recipients: []
};
async function suspended(dataElement) {
con.query('SELECT COUNT(*) as count_value FROM school.schoolinformation WHERE email = ? AND user_status = ?', [dataElement, 1], function (err, result, fields) {
// con.query('SELECT COUNT(*) as count_value FROM school.schoolinformation WHERE email = ? AND user_status = ?; SELECT COUNT(*) as count_value2 FROM school.registration_relationship WHERE teacher_email = ? AND student_email = ?', [dataElement, 1, teacher, dataElement], function (err, result, fields) {
// console.log("dataElements %s", dataElement);
if (!err) {
// console.log("Count value 1: %s",result[0][0].count_value);
// console.log("Count value 2: %s",result[1][0].count_value2);
// var suspended = result[0][0].count_value;
// var registerWithTeacher = result[1][0].count_value;
// console.log(result);
var suspended = result[0].count_value;
// Does such an email exist? (0 - NOT SUSPENDED, 1 - SUSPENDED) - suspended
// If 1 means it is a registered pair - registerWithTeacher
// has been mentioned in notification
// Is registered with the teacher
if (suspended == 0) {
console.log("pushing %s", dataElement);
retrieveValues.recipients.push(dataElement);
}
else {
responseCode = 500;
helper.writeResponse(responseCode, response, 0);
}
}
else {
responseCode = 204;
helper.writeResponse(responseCode, response, 0);
}
});
}
console.log(retrieveValues);
response.end();
})