首页 > 解决方案 > 无法读取未定义的 Nodejs 和 React 前端的属性“验证”

问题描述

我在 Nodejs 中有一项服务,其代码如下所示:

app.get('/getbackgroundPQI/:layer/:county/:year', (req, res) => {

const { layer, county,year } = req.params;
const sqc = sqlConn;
console.log(layer,county,year)
const layers = {
    MaintenanceTypes: "GetMaintnenanceTypeBkgLayer",
    LoadRestrictions: "GetLoadRestrictionBkgLayer",
    Ownership: "GetOwnershipBkgLayer",
    TotalSurfaceDepth: "GetTotalSurfaceDepth",
    SurfaceAge: "GetSurfaceAgeBkgLayer",
    SurfaceAgeBitu: "GetSurfaceAgeBkgLayerBit",
    SurfaceAgeConc: "GetSurfaceAgeBkgLayerConc",
    PublicImpact: "GetCurrentYearConsPublicImpact",
    PlannedImprovement: "GetPlannedImprovements",
    StructureCondition: "GetStructureConditions",
    StructureMaterial: "GetStructureMaterials",
    SurfaceType: "GetSurfaceTypeBkgLayer",
    SpecialConstructionHistory: "GetSpecialConstructionHistory",
    Age: "GetPerformanceAge",
    getPQI: "GetPerformancePQI",
    DropAge: "GetDropdownAge",
    DropPQI: "GetDropdownPQI"

};

const query = `
    EXEC ${layers[layer]} @county @year;
`;

const params = [sqc.paramize('county', 'year', county, year, sql.Int)];

sqc.SqlSelectParamCall(
    query, params, (data) => {
       // console.log("MY Data"+data);
        //convert data
        res.send(data);
    }, true);
});

我对服务的反应调用如下所示:

 selectBackgroundLayerPQI = (layer, e) => {
    const county = AgencyInfo.getSelectedAgencyId();
    const year = new Date().getFullYear();
    console.log(year);
    if (layer === 'none')
    {
        this.props.clickFunctions.onLoadBackgroundLayer([]);
        this.props.clickFunctions.toggleSpecialLayer(false);
        this.legendName = '';
    }
    else {
        $.ajax({
            type: 'GET',
            url: `${config.server}\\getbackgroundPQI\\${layer}\\${county}\\${year}`,
            contentType: 'application/json',
            success: (data) => {
                console.log(data);
                this.props.clickFunctions.toggleSpecialLayer(false);
                this.setState({ backgroundLegend: data[1], legendName: data[2][0].LegendName});
                this.props.clickFunctions.onLoadBackgroundLayer(data[0]);
            },
        });
    }

    this.setState({ backgroundLayer: layer });
}

现在,如果我尝试从浏览器中以http://localhost:3001/getbackgroundPQI/getPQI/77/2019访问服务的 url,我的 nodejs 服务器会显示使用 req.params 读取的参数,但是我有一个错误,即下列的:

getPQI 77 2019
info: Sql connection started...
error:  TypeError: Cannot read property 'validate' of undefined
at Request.validateParameters (C:\Users\DOTSC_souroy\Desktop\PrfCat\Geographic-Roadway-Inventory-Tool-JS\grit-server\node_modules\tedious\lib\request.js:154:36)
at Request.transformIntoExecuteSqlRpc (C:\Users\DOTSC_souroy\Desktop\PrfCat\Geographic-Roadway-Inventory-Tool-JS\grit-server\node_modules\tedious\lib\request.js:87:16)
at Connection.execSql (C:\Users\DOTSC_souroy\Desktop\PrfCat\Geographic-Roadway-Inventory-Tool-JS\grit-server\node_modules\tedious\lib\connection.js:717:15)
at C:\Users\DOTSC_souroy\Desktop\PrfCat\Geographic-Roadway-Inventory-Tool-JS\grit-server\node_modules\mssql\lib\tedious.js:1062:77
at Pool.dispense [as _dispense] (C:\Users\DOTSC_souroy\Desktop\PrfCat\Geographic-Roadway-Inventory-Tool-JS\grit-server\node_modules\generic-pool\lib\generic-pool.js:310:12)
at Pool.acquire (C:\Users\DOTSC_souroy\Desktop\PrfCat\Geographic-Roadway-Inventory-Tool-JS\grit-server\node_modules\generic-pool\lib\generic-pool.js:436:8)
at Request._acquire (C:\Users\DOTSC_souroy\Desktop\PrfCat\Geographic-Roadway-Inventory-Tool-JS\grit-server\node_modules\mssql\lib\main.js:1185:37)
at Request.TediousRequest.query (C:\Users\DOTSC_souroy\Desktop\PrfCat\Geographic-Roadway-Inventory-Tool-JS\grit-server\node_modules\mssql\lib\tedious.js:773:21)
at Request._query (C:\Users\DOTSC_souroy\Desktop\PrfCat\Geographic-Roadway-Inventory-Tool-JS\grit-server\node_modules\mssql\lib\main.js:1580:54)
at C:\Users\DOTSC_souroy\Desktop\PrfCat\Geographic-Roadway-Inventory-Tool-JS\grit-server\node_modules\mssql\lib\main.js:1540:24

我提到了我的 nodejs 和 reactjs 代码只是为了让你知道我在做什么。reactjs 函数是从前端的 Button 调用的。我有一个存储过程,如果我在数据库中执行它可以正常工作,它会返回结果。如果有人能帮助我解决这个问题,我将不胜感激。

标签: node.jsreactjs

解决方案


我将nodejs代码更改为以下内容:

app.get('/getbackgroundPQI/:county/:year', (req, res) => {

const { county,year } = req.params;
const sqc = sqlConn;
let query = 'EXEC dbo.TestGetPerformancePQI '+county+','+year;
let params = [
    sqc.paramize('county',county,sql.Int()),
    sqc.paramize('year',year,sql.Int()),

];

sqc.SqlExecuteStatementCallback(
    query, params,null,(data) => {
        res.send(data);
    });
});

我将 React 服务调用更改为如下:

 selectBackgroundLayerPQI = (layer, e) => {
    const county = AgencyInfo.getSelectedAgencyId();
    const year = new Date().getFullYear();
    console.log(year);
    if (layer === 'none')
    {
        this.props.clickFunctions.onLoadBackgroundLayer([]);
        this.props.clickFunctions.toggleSpecialLayer(false);
        this.legendName = '';
    }
    else {
        $.ajax({
            type: 'GET',
            url: `${config.server}\\getbackgroundPQI\\${county}\\${year}`,
            contentType: 'application/json',
            success: (data) => {
                console.log(data);
                this.props.clickFunctions.toggleSpecialLayer(false);
                this.setState({ backgroundLegend: data[1], legendName: data[2][0].LegendName});
                this.props.clickFunctions.onLoadBackgroundLayer(data[0]);
            },
        });
    }

    this.setState({ backgroundLayer: layer });
}

推荐阅读