如何设置回调并传递node.js模块函数变量

时间:2017-05-01 02:53:30

标签: javascript node.js function

我正在尝试将customerId,customerDescription和customerEmail传递到createCustomerProfile,后者是从单独的模块导入的。

回调变量是原始作者的默认值,我正在尝试扩展模块以获取这些额外的变量。

我能以某种方式传递它们吗?我尝试使用绑定功能,但它不断抛出错误,我不知道如何继续。之前我遇到过类似的问题,并使用对象文字语法传递类似JSON的数据,但回调变量让我失望。

index.js

const profiles = require('./authorize-net/lib/init-profile.js');
profiles.createCustomerProfile(function() {
  console.log('call back');
});

INIT-profile.js

// require some stuff...

function createCustomerProfile(callback, customerId, customerDescription, customerEmail) {

let cardNumber = '4242424242424242';
let cardExp = '0822';

// set some properties...

let customerProfileType = new ApiContracts.CustomerProfileType();
customerProfileType.setMerchantCustomerId(customerId);
customerProfileType.setDescription(customerDescription);
customerProfileType.setEmail(customerEmail);
customerProfileType.setPaymentProfiles(paymentProfilesList);

let ctrl = new ApiControllers.CreateCustomerProfileController(createRequest.getJSON());

ctrl.execute(function(){

    let apiResponse = ctrl.getResponse();

    let response = new ApiContracts.CreateCustomerProfileResponse(apiResponse);

    callback(response);
});
}

if (require.main === module) {
  createCustomerProfile(function(){
    console.log('createCustomerProfile call complete.');
  });
}

module.exports.createCustomerProfile = createCustomerProfile;

1 个答案:

答案 0 :(得分:0)

回调在参数列表中排在最后。