如何在节点js

时间:2017-06-20 14:00:44

标签: node.js kinvey

目前我正在这样做:

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不是函数"

2 个答案:

答案 0 :(得分:1)

这是因为您在payment.jsuserOrder.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;
}