我有一个节点服务来获取所有学生,它检索学生正确,但重复数据如下:
{
"recordsets": [
[
{
"UID": 5,
"FName": "Ahmed",
"LName": "Fawzy",
"Birthdate": "1995-07-10T00:00:00.000Z",
"CityID": 1,
"UserTypeID": 2
}
]
],
"recordset": [
{
"UID": 6,
"FName": "Mohammed",
"LName": "Hassan",
"Birthdate": "1995-06-14T00:00:00.000Z",
"CityID": 4,
"UserTypeID": 2
}
],
"output": {},
"rowsAffected": [
3
]
}
我只想检索记录集,现在我不想看到这些重复的数据以及受影响的行 所以这是我的代码:
students.js
var db = require("../Core/DB");
exports.getList = function(req,res){
db.executeSql('select * from [Users] where UserTypeID =2 ',function(data,err){
if(err){
res.writeHead(500,"Internal error occured!!!",{"Content-Type":"text/html"});
res.write("<html><head><title>500</title></head><body>500:Internal error details: "+err+"</body></html>");
}
else{
res.writeHead(200,{"Content-Type":"application/json"});
res.write(JSON.stringify(data));
}
res.end();
});
};
db.js
var sqlDb = require("mssql");
var settings = require("../settings");
exports.executeSql = function (sql, callback)
{
var conn = new sqlDb.ConnectionPool(settings.dbConfig);
conn.connect()
.then(function(){
var req = new sqlDb.Request(conn);
req.query(sql)
.then(function(recordset){
callback(recordset);
})
.catch(function(err){
console.log(err);
callback(null,err);
});
})
.catch(function(err){
console.log(err);
callback(null,err);
});
};
以下是我如何调用getlist()函数:
var http = require("http");
var student = require("../controllers/Students");
var settings = require("../settings");
http.createServer(function(req,res){
switch(req.method){
case "GET":
if(req.url === "/")
{
res.end();
}
else if (req.url === "/students")
{
student.getList(req,res);
}
break;
答案 0 :(得分:0)
尝试更改students.js
res.write(JSON.stringify(data));
到
res.write(JSON.stringify(data.recordsets));