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

Cannot read property "..." of undefined

在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");

然后,在一个函数中,我引用了其中一个元素:

function getAllDeptsCallForecast(res, queryParams)
{
   //some code
   var callForecastAdsl = callForecastAdsl.callForecastPerHourAndPerDay;
   //some code
}

/config/callForecastAdsl.js 文件的结构如下:

 module.exports = {
callForecastPerHourAndPerDay:`...some datas...
};

为什么我有这样的错误 500(参考函数 GetAllDeptsCallForecast 中带有 callForecastAdsl 的行)?

TypeError: Cannot read property 'callForecastPerHourAndPerDay' of undefined

您正在 隐藏 变量:

function getAllDeptsCallForecast(res, queryParams)
{
   var callForecastAdsl = callForecastAdsl.callForecastPerHourAndPerDay;
   //  ^^^^--- This is shadowing the imported `callForecastAdsl`
}

这意味着您的 require 中的 callForecastAdsl 在该函数中不可用,只能在其本地 callForecastAdsl 变量中使用,该变量最初的值为 undefined

只需使用不同的名称:

function getAllDeptsCallForecast(res, queryParams)
{
   var someOtherName = callForecastAdsl.callForecastPerHourAndPerDay;
   //  ^^^^^^^^^^^^^
}