【发布时间】:2021-08-17 06:44:51
【问题描述】:
我正在为一些基于 sqlite3 的 expressjs 应用程序编写一些数据库中间件。 这是我的目录结构:
| app
| database
index.js
| routes
index.js
database/index.js 包含:
module.exports.getLinkById = function(id) {
Database.get(`SELECT redir FROM root WHERE id = "${id}"`, [], function(err, rows) {
try {
var row = rows;
console.log(row.redir); // successfully logs the redir attr with correct value
return row;
} catch (e) {
throw e;
}
});
};
并且 routes/index.js 包含:
var database = require('../database');
console.log(database.getLinkById("afb8")); // undefined
在database/index.js文件中,根据id选择行并返回。为了调试,我console.log()ged 并记录了正确的值。
但是,即使我返回了 row 值,routes/index.js 仍将其视为未定义。
如何正确地return 值,以便我可以在routes/index.js 中访问它?
【问题讨论】: