将功能名称移至外部配置文件/模块时未定义功能错误

时间:2018-08-30 15:46:59

标签: javascript node.js

所以我在

这样的主脚本中有设置/配置代码
lobal_banks = {

    1 : {func_ : function(s){ bank_1(s)} ,  url : 'https://example.com'   , 'password' : '456'  } ,
    2 : {func_ : function(s){ bank_1(s)} ,  url : 'https://example2.com'  , 'password' : '123'   } ,
};

我像

一样使用它们
global_banks[1].func_( setting );

调用它们的函数并工作,然后我决定将配置文件移至单独的文件,因此我创建了文件config.js

var config = {};

config.banks = {

    1 : {func_ : function(s){ bank_1(s)} ,  url : 'https://example.com'   , 'password' : '456'  } ,
    2 : {func_ : function(s){ bank_1(s)} ,  url : 'https://example2.com'  , 'password' : '123'   } ,
};


module.exports = config;

然后我将主脚本更改为

var config = require('./config');
 config.banks[setting.bank_id].func_( setting );

现在我得到

ReferenceError: bank_1 is not defined

怎么了? bank_1函数是主要脚本

1 个答案:

答案 0 :(得分:0)

config.js中未定义bank_1(s)函数-您需要执行类似的操作

var config = {};

var bank_1 = function(s){
  console.log(s);
}

config.banks = {

    1 : {func_ : function(s){ bank_1(s)} ,  url : 'https://example.com'   , 'password' : '456'  } ,
    2 : {func_ : function(s){ bank_1(s)} ,  url : 'https://example2.com'  , 'password' : '123'   } ,
};


module.exports = config;