【问题标题】:Cannot read property "..." of undefined无法读取未定义的属性“...”
【发布时间】:2017-03-27 13:13:53
【问题描述】:

在 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

【问题讨论】:

    标签: javascript node.js config


    【解决方案1】:

    你正在遮蔽变量:

    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;
       //  ^^^^^^^^^^^^^
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-06
      • 2020-10-30
      • 2019-10-09
      • 1970-01-01
      相关资源
      最近更新 更多