问题描述
我正在使用Node.js,尝试使用由GET请求触发的异步函数从sqlite3数据库检索表数据。其工作原理如下:
GET请求处理程序
router.get('/',async ctx => {
const quotes = await new Quotes(dbname)
await quotes.getAll()
await quotes.close()
await ctx.render('form')
})
quotes
常量在其构造函数中打开一个sqlite3数据库(quotes.db
)。数据库打开正常,因为我已经能够创建记录。
getAll()函数
async getAll() {
const data = []
await this.db.all('SELECT * FROM quotes',(err,row) => {
console.log('here')
if(err) console.log(err)
else data.push({id: row.id,author: row.author,quote: row.quote})
})
console.log(data)
}
我的问题是回调函数永远不会运行。 “此处”未打印到控制台,并且data
仍然是一个空列表。
如果我将all
更改为each
并删除for
循环,一切都会变得很好。
预先感谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)