【发布时间】:2021-08-09 19:44:36
【问题描述】:
我正在尝试将 sql 数据转换为 json 数据,但是当文件完成时,我得到一些“记录集”这是什么以及如何摆脱它。
app.post("/Employees",function (req, res) {
const sql = require("mssql");
const config = {
server: 'HOST', //update me
user: 'user', //update me
password: 'pass', //update me
database: 'db',
trustServerCertificate: true
}
// var to store file name
var d = new Date();
var fileName = "db_"+d.getDate()+"_"+d.getMonth()+"_"+d.getTime();
sql.connect(config, function(err, data) {
if(err) console.log(err);
let sqlRequest = new sql.Request();
let sqlQuery = "select * from dbo.cust where name like 'aa%' AND surname like 'a%' AND branch='bsmtr';";
sqlRequest.query(sqlQuery, function (err, data) {
if(err) console.log(err)
console.log(data);
fs.writeFile(`json_files/${fileName}.json`, JSON.stringify(data, replacer, space), err => err && console.log(err));
sql.close();
});
});
res.send("The JSON file is downloaded please find it in the Json directory by the name "+fileName+".JSON");
});
-----这就是我获取 json 的方式 .---
{
"recordsets": [
[
{
"rowno": 294680503,
"rowno_custcrm_crm": 0,
"has_custansp_ansp": 0,
"rowno_custemt_emt": 0,
"rowno_custmtm_mtm": 0,
"rowno_custm2mc_m2mcu": 0,
"has_custext_ext": 0,
"rowno_custcloa_cloa": 0,
"has_custalrt_alrt": 0,
"rowno_custemcc_emcc": 0,
"rowno_custiop_iop": 0,
"has_custcds_cds": 0,
"has_custltv_ltv": 0,
"rowno_custoir_oir": 0,
"rowno_custorcc_orcc": 0,
"rowno_custcmps_cmps": 0
}
]
],
"output": {},
"rowsAffected": 1
}
请告诉我如何摆脱记录集、方括号、输出和行影响
【问题讨论】:
标签: javascript sql node.js json reactjs