如何从Express中的异步sqlite查询返回布尔值?

时间:2019-04-04 15:28:43

标签: javascript express callback return node-sqlite3

我正在尝试制作一些中间件来检查会话是否有效(这意味着已连接已登录的用户)。为此,我将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();

2 个答案:

答案 0 :(得分:0)

创建Promise函数并使用.thenasync/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)
})

答案 1 :(得分:-2)

可能是因为您将async添加到了回调函数。尝试删除async关键字