在 NodeJS 中循环后获取新数组值时遇到问题 [英] Having problems getting new Array Value after looping in NodeJS
本文介绍了在 NodeJS 中循环后获取新数组值时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于我是 NodeJS 的新手,我尝试了几种方法,例如在我的函数上使用 async/await,但是当我尝试打印出数组retrieveValues 时,它似乎总是为空.
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.
即使尝试 await 也无济于事,如果我在它推入数组时打印出retrieveValues,它将显示在控制台中,但在我之后执行时不会显示.
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();
})
推荐答案
也许你可以试试下面的代码:
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 = new Promise((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 teacher
if (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 here
console.log(await result);
retrieveValues.recipients.push(await result);
console.log(retrieveValues);
response.end();
});
});
希望能帮到你.
这篇关于在 NodeJS 中循环后获取新数组值时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文