承诺不等待NodeJS中的返回值

问题描述 投票:0回答:1

嗨,我是NodeJS的新手,是我先前问题(Having problems getting new Array Value after looping in NodeJS)的后续工作,我已经设法使我的代码主要用于解析电子邮件列表并将其查询到数据库,但无法获得返回值当我从NodeJS API收到响应时,我的诺言中就会体现出价值。

我正在将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)
            }
        })
    })
}

嗨,我是NodeJS的新手,是我先前问题的后续内容(在NodeJS中循环后,在获取新的数组值时遇到问题),我已经设法使我的代码主要用于解析列表...

javascript node.js api status
1个回答
0
投票

可以通过使用-“ response.statusCode = responseCode”对于旧版本Express

© www.soinside.com 2019 - 2024. All rights reserved.