【问题标题】:nodejs vertica return query as jsonnodejs vertica将查询返回为json
【发布时间】:2019-04-28 14:57:29
【问题描述】:

我正在创建一个使用 node-vertica 查询 Vertica 数据库的 nodejs express API。我可以返回结果,但如何以 json 格式转换/序列化结果?现在,结果正在返回字段和行。此外,当我注释掉我的连接关闭时,查询结果不再显示在浏览器中。这是我的连接/查询。非常感谢任何帮助,如果有比 node-vertica 更受支持的模块,请告诉我。

var express = require('express');
var router = express.Router();
Vertica = require('vertica');

var dbConfig = require('../secrets/dbconfig.js');

var config = {
  ssl: 'optional',
  interruptible: true,
  host: 'xxxx',
  user: dbConfig.iqi_user,
  password: dbConfig.iqi_password,
  database: dbConfig.iqi_DBname
};

try {
  conn = Vertica.connect(config, (err, conn) => {
    if (err) {
      console.log('error');
    } else {
      //console.log(conn);

      router.get('/', (req, res, next) => {
        conn.query('SELECT * FROM LTE_USID_CQI_2018 LIMIT 5', (err, result) => {
          if (err) throw 'hello' + err;
          res.send(result);
        });
      });
      //conn.disconnect();
    }
  });
}
catch (error) {
  console.log("Error has been caught");
  console.log(error);
}

module.exports = router;

样本输出:

{
   "fields":[
      {
         "name":"PERIOD_START_DATE",
         "tableOID":982487324,
         "tableFieldIndex":1,
         "typeOID":10,
         "type":"date",
         "size":8,
         "modifier":4294967295,
         "formatCode":0
      }
   ],
   "rows":[
      [
         "2018-01-01",
         "2018-01-01",
         "Arkansas/Oklahoma",
         "Arkansas",
         14465,
         -1.666162554,
         -0.000055538882,
         -0.00022931,
         -0.001442423085,
         -13.171986306,
         -72.151515449,
         -48.595225949,
         394158,
         396860,
         2905,
         396237,
         397289,
         397733.4534914,
         2968127857,
         590818.977,
         102111.1,
         854609.1,
         133655950,
         1606446,
         27898.3106060606
      ]
   ],
   "notices":[

   ],
   "status":""
}

【问题讨论】:

  • 你能告诉我们结果现在是如何显示的吗?
  • 我在原始帖子中添加了示例输出。
  • fyi - 行中的每个项目都有一个字段名称。我刚刚粘贴了一个示例。
  • @rfguy,你能帮我提供如何在 node.js 应用程序中集成 Vertica 驱动程序插件的步骤吗?我正在寻找这个要求。

标签: node.js api express vertica


【解决方案1】:

你好 rf 家伙,很抱歉回复晚了

这是一个将结果转换为标准 json 的 sn-p。

const res = {
  "fields": [{
    "name":" PERIOD_START_DATE"
  }, {
    "name": "STATE"
  }, {
    "name": "VALUE"
  }],
  "rows": [
    [
      "2018-01-01",                  
      "Arkansas",
      11111,         
    ], [
      "2019-01-01",                  
      "Oklahomo",
      22222,         
    ]
  ]
}
 
function mapToJSON(dbResult) {
  const fieldNames = dbResult.fields.map(field => field.name) // List of all field names
    
  return dbResult.rows.map(row => {  	
    return row.reduce((obj, item, index) => {
      const header = fieldNames[index]
      obj[header] = item
      return obj
    }, {})    
   })
}

const ans = mapToJSON(res)

console.log(ans)
  

在您的代码中,您可以执行类似的操作

res.send(mapToJSON(result));

甚至更好,因为您使用的是 Express

res.json(mapToJSON(result));

最好的问候 伯格尔

【讨论】:

  • 效果很好!感谢您的时间和帮助。你介意对我的问题进行投票吗?我正在努力建立我的声誉。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-01
  • 2019-08-22
  • 2018-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多