0
我試圖將customerId,customerDescription和customerEmail傳遞給createCustomerProfile,它是從一個單獨的模塊導入的。如何設置回調並傳遞node.js模塊函數變量
回調變量是原作者默認的,我試圖擴展模塊以獲取這些額外的變量。
我可以通過它們嗎?我嘗試使用綁定函數,但它一直拋出錯誤,我不知道如何繼續。我之前有類似的問題,並使用對象字面量語法來傳遞類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;
是否將'''index.js''中的嵌套函數作爲回調傳入? – KVNA
我應該使用'''bind'''嗎? – KVNA