[英]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.