表达:无法读取未定义的属性“ forEach”

时间:2018-12-10 18:58:21

标签: node.js sql-server express

在使用Express.js和SQL Server DB创建的api上发布时遇到了问题,直到找到下一个解决方案:

_CLEANED_PRICE_RE = re.compile('[+-]?(?:\d{1,3}[.,]?)+')
_FRACTIONAL_PRICE_RE = re.compile('^([\d.,]+)[.,](\d{1,2})$')

如您所见,我必须在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: "daUser", password: "daPass", server: "daServer", database: "daDB" }; const executeQuery = function (res, query, parameters) { sql.connect(dbConfig, function (err) { if (err) { console.log(err); res.send(err); } else { var request = new sql.Request(); parameters.forEach(function (p) { request.input(p.name, p.sqltype, p.value); }); request.query(query, function (err, result) { if (err) { console.log(err); res.send(err); sql.close(); } else { res.send(result.recordsets); sql.close(); } }); } }); } //GET ALL app.get("/api/InvoiceRequestTaxes", function (req, res) { var query = "SELECT * FROM [InvoiceRequestTaxes]"; executeQuery(res, query); }); //GET BY InvoiceRequestId app.get("/api/InvoiceRequestTaxes/:idRequest", function (req, res) { const requestId = req.params.idRequest; var query = "SELECT * FROM [InvoiceRequestTaxes] WHERE InvoiceRequestId=" + requestId; executeQuery(res, query); }); //POST app.post("/api/InvoiceRequestTaxes", function (req, res) { const parameters = [ { name: 'InvoiceRequestId', sqltype: sql.Int, value: req.body.InvoiceRequestId }, { name: 'IdProduct', sqltype: sql.Int, value: req.body.IdProduct }, { name: 'Price', sqltype: sql.Money, value: req.body.Price }, { name: 'Cost', sqltype: sql.Money, value: req.body.Cost }, { name: 'Margin', sqltype: sql.Money, value: req.body.Margin }, { name: 'BaseMarginIVA', sqltype: sql.Money, value: req.body.BaseMarginIVA }, { name: 'BaseCostIVA', sqltype: sql.Money, value: req.body.BaseCostIVA }, { name: 'IVA', sqltype: sql.Money, value: req.body.IVA }, { name: 'StatusId', sqltype: sql.Int, value: req.body.StatusId }, { name: 'DateCreated', sqltype: sql.VarChar, value: req.body.DateCreated }, { name: 'DateUpdated', sqltype: sql.VarChar, value: req.body.DateUpdated } ]; var query = "INSERT INTO [InvoiceRequestTaxes](InvoiceRequestId, IdProduct, Price, Cost, Margin, BaseMarginIVA, BaseCostIVA, IVA, StatusId, DateCreated, DateUpdated) VALUES(@InvoiceRequestId, @IdProduct, @Price, @Cost, @Margin, @BaseMarginIVA, @BaseCostIVA, @IVA, @StatusId, @DateCreated, @DateUpdated)"; executeQuery(res, query, parameters); }); const PORT = process.env.PORT || 8080 app.listen(PORT, () => { console.log(`App running on port: ${PORT}`) }); 上声明参数。我的问题是在尝试获取收益时出现的,因为控制台会显示下一条消息:

  

无法读取未定义的属性“ forEach”

在不发送任何参数的情况下如何制作get方法?

我尝试仅在这样的帖子上传递参数:

const executeQuery

但是现在我在控制台上遇到另一个错误:

  

request.input不是函数

希望有人可以帮助我。

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

2 个答案:

答案 0 :(得分:2)

在函数executeQuery中添加对参数的空检查。

const executeQuery = function (res, query, parameters) {
    sql.connect(dbConfig, function (err) {
        if (err) {
            console.log(err);
            res.send(err);
        }
        else {
            var request = new sql.Request();
            if(parameters && parameters.length>0){
                parameters.forEach(function (p) {
                    request.input(p.name, p.sqltype, p.value);
                });
            }

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

答案 1 :(得分:0)

您的parameters函数参数未定义。

const executeQuery = function (res, query, parameters) {
    ...
    parameters.forEach(function (p) {
 // ^ parameters is not defined when you call the function
        request.input(p.name, p.sqltype, p.value);
    });
}

在您的电话上,您有这个:

executeQuery(res, query);

其中parameters未定义。