首页 > 解决方案 > 如何从express中的异步sqlite查询返回布尔值?

问题描述

我正在尝试制作一些中间件来检查会话是否有效(意味着附加了登录用户)。为此,我将 sqlite3 用于节点。

我对javascript不太熟悉,所以我不知道该尝试什么。但是我尝试使用'await'查询来等待查询完成然后返回一些东西,但这只会让'Promise { undefined }'出现在控制台中。

这是运行查询的代码,我想返回 true 或 false(那些语句已经在那里),我不知道 return 语句是否在回调中工作(我认为这是一个回调)。DBmanager 返回一个工作数据库对象。

// there is a session id
    // open db connection
    let db = dbManager();

    // check the database if there is a logged in session for this browser session id
    let checkSessionSql = 'SELECT timelastactive timelastactive, sessionid id FROM session WHERE sessionid = ?';

    db.get(checkSessionSql, [session.uniqueSessionID], (err, row) => {
        if (err) {
            return console.error(err.message);
        }

        // if a row gets found then that means that this was a logged in user
        if (row) {
            // we check whether the last time this user was active was more than 48 hours ago
            if ((Math.round(new Date() / 1000)) - row.timelastactive > 172800) {
                // if this is the case then we invalidate this users session
                console.log('session older than 48 hours');
                session.destroy();
                return false;
            } else {
                // session is still valid so we update the timelastactive to the current time
                let updateTimeLastActiveSql = 'UPDATE session SET timelastactive = ? WHERE sessionid = ?';

                let currentTime = Math.round(new Date() / 1000);

                db.run(updateTimeLastActiveSql, [currentTime, row.id], function (err) {
                    return console.error(err);
                });

                console.log('updated last active time for session ' + row.id);
                return true;
            }
        }
    });

    db.close();

标签: javascriptexpresscallbackreturnnode-sqlite3

解决方案


创建 promise 函数并使用.thenor调用async/await

let db = dbManager();

// check the database if there is a logged in session for this browser session id
let checkSessionSql = 'SELECT timelastactive timelastactive, sessionid id FROM session WHERE sessionid = ?';

function somefunction(params) {
    return new Promise((res, rej) => {

        db.get(checkSessionSql, [session.uniqueSessionID], (err, row) => {
            if (err) {
                console.error(err.message);
                return rej(err);
            }

            // if a row gets found then that means that this was a logged in user
            if (row) {
                // we check whether the last time this user was active was more than 48 hours ago
                if ((Math.round(new Date() / 1000)) - row.timelastactive > 172800) {
                    // if this is the case then we invalidate this users session
                    console.log('session older than 48 hours');
                    session.destroy();
                    return res(false);
                } else {
                    // session is still valid so we update the timelastactive to the current time
                    let updateTimeLastActiveSql = 'UPDATE session SET timelastactive = ? WHERE sessionid = ?';

                    let currentTime = Math.round(new Date() / 1000);

                    db.run(updateTimeLastActiveSql, [currentTime, row.id], function (err) {
                        return console.error(err);
                    });

                    console.log('updated last active time for session ' + row.id);
                    return res(true);
                }
            }
        });
    });
}

somefunction().then((result) => {
    console.log(result)
})

推荐阅读