[英]Cannot read property “…” of undefined
In the server.js code, i have written, at the beginning : 在server.js代码中,我在一开始就写过:
var callForecastDatas = require(__dirname+"/config/callForecastDatas.js");
var callForecastAdsl = require(__dirname+"/config/callForecastAdsl.js");
var callForecastCable = require(__dirname+"/config/callForecastCable.js");
var callForecastFibre = require(__dirname+"/config/callForecastFibre.js");
var callForecastOthers = require(__dirname+"/config/callForecastOthers.js");
var callForecastOtt = require(__dirname+"/config/callForecastOtt.js");
var callForecastSatellite = require(__dirname+"/config/callForecastSatellite.js");
var callForecasttnt = require(__dirname+"/config/callForecasttnt.js");
Then, in a function, i make a reference to one of the elements : 然后,在一个函数中,我引用了其中一个元素:
function getAllDeptsCallForecast(res, queryParams)
{
//some code
var callForecastAdsl = callForecastAdsl.callForecastPerHourAndPerDay;
//some code
}
The structure of the /config/callForecastAdsl.js file is the following : /config/callForecastAdsl.js文件的结构如下:
module.exports = {
callForecastPerHourAndPerDay:`...some datas...
};
Why i have an error 500 like this (in reference to the line with callForecastAdsl in the function GetAllDeptsCallForecast)? 为什么我会有这样的错误500(参考函数GetAllDeptsCallForecast中带有callForecastAdsl的行)?
TypeError: Cannot read property 'callForecastPerHourAndPerDay' of undefined
You're shadowing the variable: 您正在隐藏变量:
function getAllDeptsCallForecast(res, queryParams)
{
var callForecastAdsl = callForecastAdsl.callForecastPerHourAndPerDay;
// ^^^^--- This is shadowing the imported `callForecastAdsl`
}
That means that the callForecastAdsl
from your require
isn't available within that function, only its local callForecastAdsl
variable, which initially has the value undefined
. 这意味着您的
require
中的callForecastAdsl
在该函数中不可用,只有其本地callForecastAdsl
变量可用,该变量的初始值为undefined
。
Just use a different name: 只需使用其他名称即可:
function getAllDeptsCallForecast(res, queryParams)
{
var someOtherName = callForecastAdsl.callForecastPerHourAndPerDay;
// ^^^^^^^^^^^^^
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.