javascript - 如何从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();
解决方案
创建 promise 函数并使用.then
or调用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)
})
推荐阅读
- java - 使用 Jersey 3.0.1 在 Java 中运行简单的 RESTful Web 服务时,显示 HTTP 状态 404 错误
- python - 不断收到 IndexError 消息?
- angular - 讨人喜欢的 rxjs 订阅者
- html - 纯 HTML/CSS 树形布局
- python - 当列长度不同时,将一个 pandas 列的值映射到另一列
- javascript - Wordpress 问题中可能存在的 jQuery
- python - (Python) 将 Playwright Page 对象传递给包装函数的函数装饰器
- sql - 打破 SQL 中的嵌套数据,跨多行的条件(类似于 R 中的 dcast)
- ios - 如何在 SwiftUI 上的最后更新位置开始手势?
- flutter - flutter 在flutter中传递两个API