目前我正在这样做:
filename:userOrder.js
const paymentGateway = require('./paymentGateway');
module.exports = {
save: function() {
// save user order record
return paymentGateway.makePayment();
},
update: function() {
// update user order record
}
}
filename:paymentGateway.js
const payment = require('./payment');
module.exports = {
makePayment: function() {
// make payment
return payment.save();
}
}
filename:payment.js
const userOrder = require('./userOrder');
module.exports = {
save: function() {
// save payment record
return userOrder.update();
}
}
return userOrder.update(); - >这一行给出错误" TypeError:userOrder.update不是函数"
答案 0 :(得分:1)
这是因为您在payment.js
和userOrder.js
之间使用循环引用。您可以在致电module.exports
之前为require
分配值,从而解决此问题,如下所示:
<强> userOrder.js 强>
const userOrder = function() { };
module.exports = userOrder;
const paymentGateway = require('./paymentGateway');
userOrder.save = function() {
// save user order record
return paymentGateway.makePayment();
}
userOrder.update = function() {
// update user order record
}
<强> payment.js 强>
const payment = function() {};
module.exports = payment;
const userOrder = require('./userOrder');
payment.save = function() {
// save payment record
return userOrder.update();
}
paymentGateway.js
不需要进行任何更改,因为这不是循环require
的一部分。
答案 1 :(得分:0)
尝试:
module.exports = {
module.save = function() {
// save user order record
return payment.save();
},
module.update = function() {
// update user order record
}
return module;
}