无法阅读财产" ..."未定义的

时间:2017-03-27 13:13:54

标签: javascript node.js config

在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

1 个答案:

答案 0 :(得分:3)

影子变量:

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

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

只需使用其他名称:

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