node.js - 如何从 Postgres 的 QueryResult 数组中提取行
问题描述
我将我的数据库响应作为一个数组获取,这是运行两个查询的结果。我需要提取结果中存在的行。如何提取具有对象的行?
或者任何其他正确获取数据的方法?请注意,我同时运行了两个查询,这就是为什么得到这样的响应。
我没有在这里粘贴任何尝试过的代码,所以请原谅我。
[ Result {
command: 'SELECT',
rowCount: 7,
oid: null,
rows:
[ [Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object] ],
fields: [ [Field], [Field], [Field] ],
_parsers:
[ [Function: parseBigInteger],
[Function: noParse],
[Function: noParse] ],
_types: TypeOverrides { _types: [Object], text: {}, binary: {} },
RowCtor: null,
rowAsArray: false },
Result {
command: 'SELECT',
rowCount: 11,
oid: null,
rows:
[ [Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object] ],
fields: [ [Field], [Field], [Field] ],
_parsers:
[ [Function: parseBigInteger],
[Function: noParse],
[Function: noParse] ],
_types: undefined,
RowCtor: null,
rowAsArray: false } ]
JSON.stringify(dbres, null, 4) 给了我
[
{
"command": "SELECT",
"rowCount": 7,
"oid": null,
"rows": [
{
"count": "11",
"a_id": "solution",
"a_description": "Solution"
}]
}]
解决方案
您需要首先查看查询的结果,然后为每个查询映射行。以下应该或多或少的伎俩
interface IQResult {
queryId: number;
queryResults: Array<Record<string, any>>;
}
const t =
dbres &&
dbres.map((res, index) => {
let qResult: IQResult = {
queryId: index,
queryResults: [],
};
res &&
res.forEach(
(r) =>
(qResult = {
...qResult,
queryResults: [...qResult.queryResults, ...r.rows],
})
);
});
推荐阅读
- eclipse - 使用 Java 11 在 Eclipse 中混合模块化和非模块化开发
- cuda - CUDA 中的 FLOP 效率
- macos - Mac Mojave .bash_profile 环境变量未拾取
- mongodb - 维护顺序的数组中的Mongo查询
- php - 如何使用正则表达式替换字符串的外部内容?
- c# - ReactiveUI:从后台线程在 UI 线程上调度任务
- swift - 如何强制位于 UITextView 垂直边界之外的字形布局?
- java - 如何使用外键创建持久类
- kubernetes - 带有 PubSub 的 GKE 上的 Google Healthcare API - INVALID_ARGUMENT
- linux - mailx不发送html电子邮件