如何在使用 express4-tedious 返回 express.js 中的路由之前更改 JSON 结果
How do I change JSON result before returning route in express.js with express4-tedious
之前在我的 express 应用程序中使用了非常有用的 pg-promise 库,现在我需要转换为 SQL 服务器。
我能找到的实现类似结果的最相似的库是 express4-tedious 包。我可以使这项工作用于简单的查询,但是,我无法弄清楚如何在 returning 结果之前操作 returned json。
app.get('/heatmapData', function(req, res) {
db.manyOrNone(`
SELECT
id
, metricname
, metricval as value
, backgroundcolor as fill
, suggestedtextcolor as color
, heatmapname
FROM
heatmapdata a
INNER JOIN
heatmapcolors b
ON
a.heatmapset = heatmapname and a.heatmapnumber=b."Order"
`)
.then(function(data) {
let bob = {}
data.map(item => {
if (bob[item.metricname] === undefined) {
bob[item.metricname] = {};
}
bob[item.metricname][item.id] = {
fill: item.fill,
color: item.color,
value: item.Value
};
bob[item.metricname].heatmapname = item.heatmapname;
})
res.status(200).json(bob);
});
});
将其转换为 SQL 服务器时,我可以使用 FOR JSON PATH 到 return 一个不错的 javascript 对象,但是,在 express4-teious 中,我具有以下语法:
req.sql('that previous big sql statement...').done(
(data)=>{
console.log('this is meant to manipulate the data');
return data
})
.into(res)
但是,这并不完全是 return我期望的结果。任何正确方向的指示都会非常有帮助!
所以在 'learning zone' 中又花了几个小时之后,我使用了更高级别的 mssql 而不是 express4-teious。这是一个很难找到的建议,但这个库在功能上更接近 pg-promise 库,尽管连接设置似乎要困难得多。
const sql = require('mssql');
var sqlconfig = require('./config.js'); //this is where the connection details are
const config = sqlconfig.config;
app.get('/myQueryData', function(req,res){
new sql.ConnectionPool(config).connect().then(pool => {
return pool.request()
.query(`
SELECT myQuery.* FROM myTables
`)
}).then(function(data){
let bob={}
data.recordset.map(item=>{
if (bob[item.metricname]=== undefined) {
bob[item.metricname]={};
}
bob[item.metricname][item.id]={fill:item.fill, color:item.color, value:item.Value};
bob[item.metricname].heatmapname = item.heatmapname;
})
res.status(200).json(bob); sql.close();
})
.catch(err => {
console.log(err)
})
sql.on('error', err => {
console.log(err)
})
});
之前在我的 express 应用程序中使用了非常有用的 pg-promise 库,现在我需要转换为 SQL 服务器。
我能找到的实现类似结果的最相似的库是 express4-tedious 包。我可以使这项工作用于简单的查询,但是,我无法弄清楚如何在 returning 结果之前操作 returned json。
app.get('/heatmapData', function(req, res) {
db.manyOrNone(`
SELECT
id
, metricname
, metricval as value
, backgroundcolor as fill
, suggestedtextcolor as color
, heatmapname
FROM
heatmapdata a
INNER JOIN
heatmapcolors b
ON
a.heatmapset = heatmapname and a.heatmapnumber=b."Order"
`)
.then(function(data) {
let bob = {}
data.map(item => {
if (bob[item.metricname] === undefined) {
bob[item.metricname] = {};
}
bob[item.metricname][item.id] = {
fill: item.fill,
color: item.color,
value: item.Value
};
bob[item.metricname].heatmapname = item.heatmapname;
})
res.status(200).json(bob);
});
});
将其转换为 SQL 服务器时,我可以使用 FOR JSON PATH 到 return 一个不错的 javascript 对象,但是,在 express4-teious 中,我具有以下语法:
req.sql('that previous big sql statement...').done(
(data)=>{
console.log('this is meant to manipulate the data');
return data
})
.into(res)
但是,这并不完全是 return我期望的结果。任何正确方向的指示都会非常有帮助!
所以在 'learning zone' 中又花了几个小时之后,我使用了更高级别的 mssql 而不是 express4-teious。这是一个很难找到的建议,但这个库在功能上更接近 pg-promise 库,尽管连接设置似乎要困难得多。
const sql = require('mssql');
var sqlconfig = require('./config.js'); //this is where the connection details are
const config = sqlconfig.config;
app.get('/myQueryData', function(req,res){
new sql.ConnectionPool(config).connect().then(pool => {
return pool.request()
.query(`
SELECT myQuery.* FROM myTables
`)
}).then(function(data){
let bob={}
data.recordset.map(item=>{
if (bob[item.metricname]=== undefined) {
bob[item.metricname]={};
}
bob[item.metricname][item.id]={fill:item.fill, color:item.color, value:item.Value};
bob[item.metricname].heatmapname = item.heatmapname;
})
res.status(200).json(bob); sql.close();
})
.catch(err => {
console.log(err)
})
sql.on('error', err => {
console.log(err)
})
});