javascript - 链接函数不能同步工作
问题描述
我查询数据库如下:
const testMe = async () => {
const index = await _fetchNextIndex()
if (index === undefined) {
throw new Error('Next index could not fetched')
}
return 'Success'
}
const _fetchNextIndex = async () => {
return db.query("SELECT nextval(pg_get_serial_sequence('testme_table', 'id'))", (err, result) => {
if (err) {
throw new Error('Index could not generated')
}
return result.rows[0]
})
}
db
定义为:
const {Pool} = require('pg')
const pool = new Pool()
module.exports = {
query: async (text, params, callback) => {
return await pool.query(text, params, callback)
}
}
我的代码不能同步工作。index
未定义并引发错误。但是,当我调试应用程序时,我看到该行在一段时间后被调用:
return result.rows[0]
我想念什么?
编辑1:我已经添加async
并await
进入query
功能。
编辑 2:我还删除了回调。
const {Pool} = require('pg')
const pool = new Pool()
module.exports = {
query: async (text, params) => {
return await pool.query(text, params)
}
}
解决方案
推荐阅读
- java - JMS 侦听器正在自动关闭
- swagger - Swagger 正在为 get 方法添加额外的 body 参数
- c++ - 使用 cmake 链接错误
- python - 从 Python 脚本调用 autoit 脚本
- nskeyedarchiver - 如何在 iOS 12 中替换 NSKeyedArchiver 的初始化程序 init(forWritingWith:) 来编码 CKRecord 的元数据
- c++ - 使用 C++ 将一维数组转换为二维字符串数组
- merge - 按列名称“名称”合并两个数据集
- python - 无法从类中调用函数以在 Python 中绘制椭圆
- postgresql - 全文搜索不产生结果
- uwp - 加载到信息亭模式时禁用win 10“请稍候”蓝屏