2016-12-14 29 views
0

我打電話function,並在nodefunction的內部輸入callbackfunction發射並完成其任務,但我無法弄清楚如何從callback中檢索response處理來自回撥函數的響應

foo() { 
    merchant.createCustomerProfile(cardNumber, expDate, email, id, function callback(){ 
       // How do I retrieve the response from the callback?     
       // console.log(callback); 
      }); 
    //so I can pass it out here and use it 
} 

functioncallback實際上returnscallback。它看起來像這樣,

function createCustomerProfile(cardNumber, expDate, email, id, callback) { 
    ctrl.execute(function(){ 

     var apiResponse = ctrl.getResponse(); 

     var response = new ApiContracts.CreateCustomerProfileResponse(apiResponse); 
     if(response != null) 
     { 
      if(response.getMessages().getResultCode() == ApiContracts.MessageTypeEnum.OK) 
      { 
       console.log('Successfully created a customer profile with id: ' + response.getCustomerProfileId()); 
      } 
      else 
      { 
       console.log('Result Code: ' + response.getMessages().getResultCode()); 
       console.log('Error Code: ' + response.getMessages().getMessage()[0].getCode()); 
       console.log('Error message: ' + response.getMessages().getMessage()[0].getText()); 
      } 
     } 
     else 
     { 
      console.log('Null response received'); 
     } 

     callback(response); 
    }); 
} 

我試圖獲得resultCode,應在回調函數的響應。

回答

1

你可以試試這個:

foo() { 
    merchant.createCustomerProfile(cardNumber, expDate, email, id, function callback(response){ 
       // How do I retrieve the response from the callback?     
       // console.log(callback); 
       console.log(response); 
      }); 
    //so I can pass it out here and use it 
} 
相關問題