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

标签 javascript node.js npm

由于我是 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/

相关文章:

Node.js "Server listening on port 3000",但我无法在浏览器中查看它?

node.js - Jimp 及其缓冲区数据无法按预期使用 puppeteer 生成 PDF

javascript - 根据发出请求的位置获取本地日期

javascript - 无法访问对象属性,即使它显示在控制台日志中

javascript - 带有 BlendColor 过滤器的 IE 上的透明度问题

npm - 解决这个问题的NPM命令应该是什么?

npm - 使用npm run-script运行时,ESLint会产生不同的输出

angular - 如何删除 Angular 6 中的 dist 文件?

javascript - 动态加载多边形

javascript - 找到用户在 JS 中选择的正确按钮?