繁体   English   中英

等待节点js中的sqlite3查询

[英]wait for sqlite3 query in node js

我正在创建一个数组 JSON,其中包含一些从数据库中获取的测试名称和问题,如下所示:

db.all(sql_instruction, [], (err, rows) => {
        if (err) {
            console.log(err)
        }

        rows.forEach(test=>{
            let test_name = test.test_name;
            let question_array = [];
            sql_instruction = `SELECT * from questions where test_id = ?`;
            db.all(sql_instruction, [test.id], (err, rows) => {
                if (err) {
                    console.log(err);
                }
                rows.forEach(question=> {
                    question_array.push(question);
                });
                test_array.push(JSON.stringify({'test_name':test_name, questions:question_array}));
            });
        });
    });

如果我尝试访问第一个 db.all() 之外的变量“test_array”,我会得到一个空数组,因为 function 的异步特性。

“等待”完成“test_array”变量以在应用程序中进一步使用它的最佳方法是什么?

我们可以使用 Promise 和 async/await 糖语法使异步代码看起来像同步代码。 在下面的代码中,我创建了一个 function getAllPromise以将db.all方法包装在 Promise 中。 然后我可以“等待”我的getAllPromise function。

function getAllPromise(query, params) {
    return new Promise((resolve, reject) => {

        db.all(query, params, (err, rows) => {

            if(err) {
                
                // case error
                reject(err);
            }

            // "return" the result when the action finish
            resolve(rows);
        })
    })
}

async function getTestWithQuestions() { // your code

    try {
        let sql_instruction = ""; // your SQL query;
        const rows = await getAllPromise(sql_instruction, []);
        let test_array = [];

        for(let i = 0; i < rows.length; i = i + 1) {

            const test = rows[i];
            let test_name = test.test_name;
            let question_array = [];
            sql_instruction = `SELECT * from questions where test_id = ?`;

            const question_rows = await getAllPromise(sql_instruction, [test.id]);
            question_rows.forEach(question=> {
                question_array.push(question);
            });

            test_array.push(JSON.stringify({'test_name':test_name, questions:question_array}))
        }

        return test_array;

    } catch(error) {
        console.log(error);
        throw error;
    }
}

// using
// you need to add the keyword "async" in the function that call getTestWithQuestions, then :

const testWithQuestions = await  getTestWithQuestions(); // testWithQuetions is `test_array`

// if you don't want to add async keyword, then : 
getTestWithQuestions()
.then(testWithQuestions => console.log(testWithQuestions))
.catch(error => console.log(error));

您可以在此处找到有关回调、Promise 和 async/await 的更多信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM