Having Problems Getting New Array Value After Looping In Nodejs
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
Solution 1:
That last console.log
runs before pushing.
You are passing function as a callback, so it will run after the operation ends.
I cleaned your code a little bit and moved the console.log
.
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: []
};
con.query('SELECT COUNT(*) as count_value FROM school.schoolinformation WHERE email = ? AND user_status = ?', [dataElement, 1], function (err, result, fields) {
if (!err) {
var suspended = result[0].count_value;
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();
});
})
Solution 2:
Maybe you can try this code below:
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: []
};
const result = newPromise((resolve, reject) => { 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 teacherif (suspended == 0) {
console.log("pushing %s", dataElement);
resolve(dataElement);
}
else {
responseCode = 500;
helper.writeResponse(responseCode, response, 0);
}
}
else {
responseCode = 204;
helper.writeResponse(responseCode, response, 0);
}
});
// do some stuff hereconsole.log(await result);
retrieveValues.recipients.push(await result);
console.log(retrieveValues);
response.end();
});
});
I hope it can help you.
Post a Comment for "Having Problems Getting New Array Value After Looping In Nodejs"