为什么Express api返回两个具有相同数据的对象?

时间:2018-12-06 20:38:03

标签: node.js sql-server express

我正在使用Express和SQL Server作为数据库来创建API。我创建了一个post方法,它可以正常工作,但是get方法存在问题,因为它返回的是两个具有相同数据的对象。这是我的代码:

const express = require('express');
const bodyParser = require('body-parser');
const sql = require('mssql');
const app = express();

app.use(bodyParser.json());

app.use(function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, contentType,Content-Type, Accept, Authorization");
    next();
});


const dbConfig = {
    user: "theUser",
    password: "thePass",
    server: "theServer",
    database: "theDB"
}

const executeQuery = function (res, query) {
    sql.connect(dbConfig, function (err) {
        if (err) {
            console.log(err);
            res.send(err);
        }
        else {
            // create Request object
            var request = new sql.Request();


            // query to the database
            request.query(query, function (err, result) {
                if (err) {
                    console.log(err);
                    res.send(err);
                }
                else {
                    res.send(result);
                    sql.close();
                }
            });
        }
    });
}

//Get All
app.get("/api/HolidayBaseApi", function (req, res) {
    var query = "SELECT * FROM [HolidaysBase]";
    executeQuery(res, query);
    //executeQuery(res, query);
});


app.post("/api/HolidayBaseApi", function (req, res) {
    var query = "INSERT INTO [HolidaysBase] (EmployeeNumber, PeriodBegin, PeriodEnd, WorkedYears, DaysPerYear, TakenDays, RemainingDays) VALUES ('"+req.body.EmployeeNumber+"','"+req.body.PeriodBegin+"','"+req.body.PeriodEnd+"','"+req.body.WorkedYears+"','"+req.body.DaysPerYear+"','"+req.body.TakenDays+"','"+req.body.RemainingDays+"')";
    executeQuery(res, query);
});


const PORT = process.env.PORT || 8080

app.listen(PORT, () => {
    console.log("App now running on port", PORT);
});

我正在测试邮递员,下一个回报是:

    {
    "recordsets": [
        [
            {
                "Id": 1,
                "EmployeeNumber": 4,
                "PeriodBegin": "2018-04-01T00:00:00.000Z",
                "PeriodEnd": "2019-03-31T00:00:00.000Z",
                "WorkedYears": 6,
                "DaysPerYear": 18,
                "TakenDays": 10,
                "RemainingDays": 8
            },
            {
                "Id": 2,
                "EmployeeNumber": 5,
                "PeriodBegin": "2018-08-02T00:00:00.000Z",
                "PeriodEnd": "2018-07-31T00:00:00.000Z",
                "WorkedYears": 6,
                "DaysPerYear": 18,
                "TakenDays": 9,
                "RemainingDays": 9
            },
            {
                "Id": 5,
                "EmployeeNumber": 9,
                "PeriodBegin": "2018-10-15T00:00:00.000Z",
                "PeriodEnd": "2019-10-15T00:00:00.000Z",
                "WorkedYears": 6,
                "DaysPerYear": 18,
                "TakenDays": 0,
                "RemainingDays": 18
            }
        ]
    ],
    "recordset": [
        {
            "Id": 1,
            "EmployeeNumber": 4,
            "PeriodBegin": "2018-04-01T00:00:00.000Z",
            "PeriodEnd": "2019-03-31T00:00:00.000Z",
            "WorkedYears": 6,
            "DaysPerYear": 18,
            "TakenDays": 10,
            "RemainingDays": 8
        },
        {
            "Id": 2,
            "EmployeeNumber": 5,
            "PeriodBegin": "2018-08-02T00:00:00.000Z",
            "PeriodEnd": "2018-07-31T00:00:00.000Z",
            "WorkedYears": 6,
            "DaysPerYear": 18,
            "TakenDays": 9,
            "RemainingDays": 9
        },
        {
            "Id": 5,
            "EmployeeNumber": 9,
            "PeriodBegin": "2018-10-15T00:00:00.000Z",
            "PeriodEnd": "2019-10-15T00:00:00.000Z",
            "WorkedYears": 6,
            "DaysPerYear": 18,
            "TakenDays": 0,
            "RemainingDays": 18
        }
    ],
    "output": {},
    "rowsAffected": [
        3
    ]
}

如您所见,它返回相同对象的两倍。有人知道为什么它返回两个记录集,我该如何解决?从昨天开始我一直在搜索,但是没有此行为的信息。

我正在使用Express.js,Node和SQL Server。

1 个答案:

答案 0 :(得分:0)

我自己找到了解决方案。在我的executeQuery上,只需进入结果中的recordsets

const executeQuery = function (res, query) {
    sql.connect(dbConfig, function (err) {
        if (err) {
            console.log(err);
            res.send(err);
        }
        else {
            // create Request object
            var request = new sql.Request();

            // query to the database
            request.query(query, function (err, result) {
                if (err) {
                    console.log(err);
                    res.send(err);
                }
                else {
                    res.send(result.recordsets);
                    sql.close();
                }
            });
        }
    });
}

这样做,我在对象内部有一个对象作为响应:

[
  [
    {
        "Id": 1,
        "EmployeeNumber": 4,
        "PeriodBegin": "2018-04-01T00:00:00.000Z",
        "PeriodEnd": "2019-03-31T00:00:00.000Z",
        "WorkedYears": 6,
        "DaysPerYear": 18,
        "TakenDays": 10,
        "RemainingDays": 8
    },
    {
        "Id": 2,
        "EmployeeNumber": 5,
        "PeriodBegin": "2018-08-02T00:00:00.000Z",
        "PeriodEnd": "2018-07-31T00:00:00.000Z",
        "WorkedYears": 6,
        "DaysPerYear": 18,
        "TakenDays": 9,
        "RemainingDays": 9
    },
    {
        "Id": 5,
        "EmployeeNumber": 9,
        "PeriodBegin": "2018-10-15T00:00:00.000Z",
        "PeriodEnd": "2019-10-15T00:00:00.000Z",
        "WorkedYears": 6,
        "DaysPerYear": 18,
        "TakenDays": 0,
        "RemainingDays": 18
    },
    {
        "Id": 6,
        "EmployeeNumber": 37,
        "PeriodBegin": "2018-07-27T00:00:00.000Z",
        "PeriodEnd": "2019-07-27T00:00:00.000Z",
        "WorkedYears": 4,
        "DaysPerYear": 16,
        "TakenDays": 3,
        "RemainingDays": 13
    },
    {
        "Id": 7,
        "EmployeeNumber": 77,
        "PeriodBegin": "2018-01-30T00:00:00.000Z",
        "PeriodEnd": "2019-01-30T00:00:00.000Z",
        "WorkedYears": 6,
        "DaysPerYear": 18,
        "TakenDays": 18,
        "RemainingDays": 0
    }
  ]
]

因此,最终的解决方案是进入对象内部并将第一个索引带到request.object上:

request.query(query, function (err, result) {
                    if (err) {
                        console.log(err);
                        res.send(err);
                    }
                    else {
                        res.send(result.recordsets[0]);
                        sql.close();
                    }
                });