gpt4 book ai didi

javascript - 在 NodeJS 中循环后获取新数组值时遇到问题

转载 作者:太空宇宙 更新时间:2023-11-04 01:18:53 27 4
gpt4 key购买 nike

由于我是 NodeJS 新手,我尝试了几种方法,例如在我的函数上使用 async/await,但是当我尝试打印数组retrieveValues 时,它似乎总是为空。

即使尝试等待也无济于事,如果我在插入数组时打印出retrieveValues,它将显示在控制台中,但当我之后执行时不会显示。

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();

})

最佳答案

最后一个 console.log 在推送之前运行。您将函数作为回调传递,因此它将在操作结束后运行。我稍微清理了你的代码并移动了 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();
});

})

关于javascript - 在 NodeJS 中循环后获取新数组值时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59996429/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com