繁体   English   中英

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM