javascript 无法等待Node.js中的sqlite3.Database.get()函数完成

xxhby3vn  于 2022-11-27  发布在  Java
关注(0)|答案(2)|浏览(125)

我正在使用node-sqlite3处理node.js中的一些基本的异步/等待问题。
我的目标是从SQLite DB中选择一些值,检查它是否满足一些条件,并在满足条件时采取一些措施。

const sqlite3 = require('sqlite3').verbose();
main();

async function main() {
    let ordersDb = await createDbConnection('./ProcessedOrders.db');
    var orderProcessed = await orderAlreadyProcessed(ordersDb, "555");
    console.log("orderProcessed = " + orderProcessed);
    if (!orderProcessed) {
        console.log("So condition is met!");
    }
}

async function orderAlreadyProcessed(ordersDb, orderNumberStr) {
    console.log('starting orderAlreadyProcessed function'); //DEBUG
    var result;
    var query = 'select count(SoldOrderNumber) as "recsCount" from ProcessedSoldOrders where SoldOrderNumber = ?;';
    await ordersDb.get(query
    ,[orderNumberStr]
    ,(err, row) => {
        console.log('Row with count = ' + row); //DEBUG
        console.log('row.recsCount = ' + row.recsCount); //DEBUG
        result = typeof row !== 'undefined' && row.recsCount > 0;
    });
    console.log('Returning ' + result); //DEBUG
    return result;
}

async function createDbConnection(dbFileName) {
    let db = new sqlite3.Database(dbFileName, (err) => {
        if (err) {
            console.log(err.message);
        }
    });
    return db;
}

但是我得到的是代码进一步执行,而不是等待Database.get()方法!因此,我在控制台中看到了以下输出:

starting orderAlreadyProcessed function
Returning undefined
orderProcessed = undefined
So IF condition met!
Row with count = [object Object]
row.recsCount = 1

正如我们所看到的,我们过早地从orderAlreadyProcessed返回,返回值= 'undefined'。因此,条件得到满足,采取行动,然后才返回Database.get()。但是,如果适当地等待,条件就不会得到满足。
如何让它等待结果值?

bfnvny8b

bfnvny8b1#

因为您要使用async/await,而node-sqlite3 (sqlite3)程式库不支援Promise API,所以您需要使用node-sqlite (sqlite)程式库,它是sqlite3的 Package 函数,并加入了Promise API的支援。然后,您的程式码看起来会像这样:

const sqlite3 = require('sqlite3');
const { open } = require('sqlite');

async function main() {
    try {
        sqlite3.verbose();
        const ordersDb = await createDbConnection('./ProcessedOrders.db');
        const orderProcessed = await orderAlreadyProcessed(ordersDb, "555");
        console.log("orderProcessed = " + orderProcessed);
        if (!orderProcessed) {
            console.log("So condition is met!");
        }
    } catch (error) {
        console.error(error);
    }
}

async function orderAlreadyProcessed(ordersDb, orderNumberStr) {
    try {
        console.log('Starting orderAlreadyProcessed function');
        const query = 'SELECT COUNT(SoldOrderNumber) as `recsCount` from ProcessedSoldOrders where SoldOrderNumber = ?;'
        const row = await ordersDb.get(query, [orderNumberStr]);
        console.log('Row with count =', row);
        console.log('row.recsCount =', row.recsCount);
        const result = typeof row !== 'undefined' && row.recsCount > 0;
        console.log('Returning ' + result);
        return result;
    } catch (error) {
        console.error(error);
        throw error;
    }
}

function createDbConnection(filename) {
    return open({
        filename,
        driver: sqlite3.Database
    });
}

main();

我特意没有删除console.log和代码的其他部分,以免混淆程序的原始逻辑。

cx6n0qe3

cx6n0qe32#

如果我们不使用另一个图书馆
那么我们可以返回一个new Promise函数&使用await,如下所示:

:以下是INSERT/run的示例,而不是SELECT/get,但promise/await的工作原理相同

第一个

相关问题