node.js - 无法读取未定义的 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 调用的。我有一个存储过程,如果我在数据库中执行它可以正常工作,它会返回结果。如果有人能帮助我解决这个问题,我将不胜感激。
解决方案
我将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 });
}
推荐阅读
- html - 如何在同一页面的不同选项卡中显示多个幻灯片
- openshift - 如何在 minishift 中部署 dockerhub 或 redhat 容器目录(外部注册表)映像
- c++ - fscanf:如果没有返回 EOF,是否可能出现错误或 EOF?
- python - 如何读取/解析目录中的所有 yaml 文件?
- ubuntu - 无效的 url(404 域子目录)使用 apache2 重定向到域而不使用 .htaccess
- python - 编程问题中的格式化问题
- python - 如何使用 Selenium 从先前插入的输入中提取文本?
- robotframework - 机器人框架-移动自动化测试用例
- r - 导入包变量定义
- git - 在 Git 中,如何在分支上进行新提交,其树等于另一个提交的树