javascript - NodeJS 中 Promise 不等待返回值

标签 javascript node.js api status

嗨,因为我是 NodeJS 的新手,作为我上一个问题 ( Having problems getting new Array Value after looping in NodeJS ) 的后续问题,我已经设法让我的代码主要用于解析电子邮件列表并将其查询到数据库,当我从 NodeJS API 收到响应时,无法获取我的 promise 中的返回值。

我的数据库使用 MySQL 2.18.1 和 4.17.1

有什么想法可以解决吗?已经尝试了几个小时了。

日志:

IM OUT HERE
Promise { { recipients: [] } }
{ recipients: [] }

retrieve_for_notification.js

async function processEmails(emails) {
    var retrieveValues = {
        recipients: []
    };

    // emails consist of those who were mentioned/notified, check condition whether they are eligible to be placed in receipients list
    for (var i = 0; i < emails.length; i++) {
        console.log(emails[i]);

        // 1 - check for suspended
        // 2 - check whether teacher and student pair is registered
        var sql1 = 'SELECT COUNT(*) as count_value FROM school.schoolinformation WHERE email = ? AND user_status = ?';
        var sql2 = 'SELECT COUNT(*) as count_value2 FROM school.registration_relationship WHERE teacher_email = ? AND student_email = ?';
        var sql3 = 'SELECT COUNT(*) as count_value3 FROM school.schoolinformation WHERE email = ? AND user_type = ?';

        var sqlvalues1 = [emails[i], 1];
        var sqlvalues2 = [teacher, emails[i]];
        var sqlvalues3 = [emails[i], 0];

        // check for suspended
        con.pool.query(sql1, sqlvalues1, async function (err1, result1) {
            if (err1) throw err1;
            var res1 = await getResult(sql1, sqlvalues1)
            // console.log("(1) res value is %s", res1[0].count_value);
            if (res1 > 0) return; // if result found skip to next email

            // check whether teacher and student pair is registered
            con.pool.query(sql2, sqlvalues2, async function (err2, result2) {
                if (err2) throw err2;
                var res2 = await getResult(sql2, sqlvalues2)

                // teacher and student pair is not registered
                if (res2 == 0) {
                    // check whether student mentioned is a valid student
                    con.pool.query(sql3, sqlvalues3, async function (err3, result3) {
                        if (err3) throw err3;
                        var res3 = await getResult(sql3, sqlvalues3)

                        // student is valid
                        if (res3 == 0) {
                            retrieveValues.recipients.push(sqlvalues3[0]);
                        }
                    });
                }
                else {
                    retrieveValues.recipients.push(sqlvalues2[0]);
                }
            });
        });
    };
    return recipientsList;
}

var recipientsList = processEmails(emails);

console.log("IM OUT HERE");
console.log(recipientsList);
// Resolve promise and response send, not using helper for this
var p2 = Promise.resolve(recipientsList);
p2.then(function(v) {
    console.log(v);
    response.write(JSON.stringify(v, null, 3));
    response.send.bind(response);
    response.end();
}, function(e) {
    console.error(e); // TypeError: Throwing
});

function getResult(sql, sqlvalues) {
    // console.log("getResult SQL Query: %s", sql);
    return new Promise(function (resolve, reject) {
        con.pool.query(sql, sqlvalues, function (err, result) {
            if (err) {
                reject(err)
            } else {
                resolve(result)
            }
        })
    })
}

最佳答案

可以使用 - “response.statusCode = responseCode”旧版本express

关于javascript - NodeJS 中 Promise 不等待返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60005146/

相关文章:

javascript - Jquery - 为常见任务编写函数时出现问题

node.js - 使用node js需要从Facebook下载图像

node.js - npm WARN install 拒绝安装 hapi 作为自身的依赖

javascript - 如何使用 Node.js 的 wpapi 创建新的 WordPress 帖子?

api - Azure 函数输出缓存

javascript - MEAN 应用程序 : Can't receive image on back end, 上传失败

javascript - 在moment js中从数组中删除重复项

javascript - 返回值未传递给其他函数

node.js - Nodemailer 可以作为独立的 SMTP 服务器吗?

ios - Swift 4 - 使用哪种类型的数组 - 将 JSON 响应解析为数组