I have four JavaScript files as follows:
我有四个JavaScript文件如下:
API.js
var controller = require('./Controller');
var apifunction = new function(){
controller.a(function(result) {
console.log("result: " + result)
})}
Controller.js
var bll = require('./BLL.js');
module.exports =
{
a: function(callback){
bll.b(function(result){
callback(result);
})
}
c: function(callback){
callback(result);
}
}
}
BLL.js
var controller = require('./Controller.js');
module.export =
{
b: function(callback){
controller.c(function(result){
callback(result);
})
}
}
So the API function that calls the a function in the controller which again calls the b function in the BLL.
因此API函数调用控制器中的一个函数,该函数再次调用BLL中的b函数。
In the BLL it calls the c function in the controller again but then I get an Error: controller.c is not a function.
在BLL中它再次调用控制器中的c函数,但后来我得到一个错误:controller.c不是一个函数。
Any ideas?
2 个解决方案
#1
0
Controller.js and BLL.js "require" each other, that's the problem, one will get a empty module(placeholder).
Controller.js和BLL.js彼此“需要”,这就是问题,一个人将获得一个空模块(占位符)。
You can refactor it, remove the bidirectional "require", such as pass one module as parameter to the other.
您可以重构它,删除双向“require”,例如将一个模块作为参数传递给另一个。
#2
0
Consider seperating out the function controller.c into a seperate file...E.g. commonC.js
考虑将函数controller.c分离成一个单独的文件...例如。 commonC.js
CommonC.js
module.exports =
{
c: function(callback){
callback(result);
}
}
}
Controller.js
var bll = require('./BLL.js');
module.exports =
{
a: function(callback){
bll.b(function(result){
callback(result);
})
}
}
BLL.js
var controller = require('./Controller.js');
var commonC = require('./CommonC.js');
module.export =
{
b: function(callback){
commonC.c(function(result){
callback(result);
})
}
}
Alternatively, if refactoring is not an option (you should refactor), you can put the require inside the function so that it executes when the function is invoked.
或者,如果重构不是一个选项(你应该重构),你可以将require放在函数中,以便在调用函数时执行。
Controller.js
module.exports =
{
a: function(callback){
var bll = require('./BLL.js');
bll.b(function(result){
callback(result);
})
}
c: function(callback){
callback(result);
}
}
}
#1
0
Controller.js and BLL.js "require" each other, that's the problem, one will get a empty module(placeholder).
Controller.js和BLL.js彼此“需要”,这就是问题,一个人将获得一个空模块(占位符)。
You can refactor it, remove the bidirectional "require", such as pass one module as parameter to the other.
您可以重构它,删除双向“require”,例如将一个模块作为参数传递给另一个。
#2
0
Consider seperating out the function controller.c into a seperate file...E.g. commonC.js
考虑将函数controller.c分离成一个单独的文件...例如。 commonC.js
CommonC.js
module.exports =
{
c: function(callback){
callback(result);
}
}
}
Controller.js
var bll = require('./BLL.js');
module.exports =
{
a: function(callback){
bll.b(function(result){
callback(result);
})
}
}
BLL.js
var controller = require('./Controller.js');
var commonC = require('./CommonC.js');
module.export =
{
b: function(callback){
commonC.c(function(result){
callback(result);
})
}
}
Alternatively, if refactoring is not an option (you should refactor), you can put the require inside the function so that it executes when the function is invoked.
或者,如果重构不是一个选项(你应该重构),你可以将require放在函数中,以便在调用函数时执行。
Controller.js
module.exports =
{
a: function(callback){
var bll = require('./BLL.js');
bll.b(function(result){
callback(result);
})
}
c: function(callback){
callback(result);
}
}
}