2017-03-27 177 views
0

在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 

回答

3

陰影變量:

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

這意味着,從您的requirecallForecastAdsl不是函數中使用,只是其局部callForecastAdsl變量,該變量的初始值爲undefined

只需使用一個不同的名稱:

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

謝謝,這個問題就解決了! –

相關問題