You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用GET方法调用Node.js学生查询接口出现重复结果求助

Fix: Return Only recordset Data from MSSQL Query in Node.js

I see the issue here—when using the mssql library's query() method, it returns a full result object packed with metadata like recordsets, rowsAffected, and the actual student data you need stored in the recordset property. To get just the clean list of students without redundant fields, you only need to extract that specific property from the result.

Here are two straightforward ways to fix this:

Option 1: Handle Extraction in the Database Layer (db.js)

This keeps your controller code clean by centralizing the data formatting at the database level:

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(result){ // Rename variable to `result` for clarity
 // Pass only the recordset data to the callback
 callback(result.recordset);
 })
 .catch(function(err){
 console.log(err);
 callback(null,err);
 });
 })
 .catch(function(err){
 console.log(err);
 callback(null,err);
 });
};

Option 2: Handle Extraction in the Student Controller (students.js)

If you want to keep the DB layer generic (for other queries that might need full result metadata), process the data in your controller instead:

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"});
 // Extract only the recordset from the full result object
 res.write(JSON.stringify(data.recordset));
 }
 res.end();
 });
};

Why This Works

The mssql library's query() method resolves to a result object with several key properties:

  • recordset: The array of rows returned by your query (this is exactly the student data you want)
  • recordsets: An array of all record sets (useful for multi-statement queries)
  • rowsAffected: The number of rows impacted by the query
  • output: Output parameters from stored procedures

By targeting result.recordset (or data.recordset in the controller), you strip out all extra metadata and return only the clean list of student records.

After making either change, your response will look like this:

[
  {
    "UID": 5,
    "FName": "Ahmed",
    "LName": "Fawzy",
    "Birthdate": "1995-07-10T00:00:00.000Z",
    "CityID": 1,
    "UserTypeID": 2
  },
  {
    "UID": 6,
    "FName": "Mohammed",
    "LName": "Hassan",
    "Birthdate": "1995-06-14T00:00:00.000Z",
    "CityID": 4,
    "UserTypeID": 2
  }
]

内容的提问来源于stack exchange,提问作者Mohammed Ehab

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:06:01