如何在Node.js中的另一个回调函数中调用HTTP回调

时间:2018-10-15 11:02:00

标签: node.js callback

我有两个回调函数,我想在If else loop中使用它们。当我分别执行两个函数时,它们将起作用并根据需要产生输出。但是当我将其合并将无法工作时,我没有收到任何错误,控件未达到第二个功能。 下面是代码

var API_PATH = "/api/v1/secure/weeklyFeatures"
var host_url = 'XXXX'
var port_num = "8080"; 
var https = require('http');
var request=require('request');
var Client = require('node-rest-client').Client;
var request=require('request');
var testClient = new Client();


function httpsGet(myData, callback) {
    var options = {
            path: API_PATH +'/mobile/'+ myData,
            method: 'GET',
            headers:{
                'Accept':'application/json',
                'Authorization-Type':'Native',
                'Content-Type': 'application/json'
            },
        };
    var req = https.request(options, res => {
        res.setEncoding('utf8');
        var returnData = "";

        res.on('data', chunk => {
            returnData = returnData + chunk;
        });

        res.on('end', () => { // received the raw return data in the returnData variable.
        console.log("httpsGet::returnData: " + JSON.stringify(returnData));
        callback(returnData,res.statusCode);  // this will execute whatever function the caller defined, with one argument
        });
    });
    req.end();
}//End httpsGet

var myRequest = "9886876874";
httpsGet(myRequest, (myResult,statusCode) => {
    console.log("sent     : " + myRequest);
    console.log("received : " + myResult);
    var userDetails;
    var totalMessages;
    if (statusCode == 200)
    {
    userDetails = JSON.parse(myResult);
        console.log("User Details = " + JSON.stringify(userDetails));
        totalMessages= Object.keys(userDetails).length;
        if (totalMessages == 0)
        {
           
            console.log("Success" );

            httpsGetClaims(myRequest, (myResult,statusCode) => {
                console.log("sent     : " + myRequest);
                console.log("received : " + myResult);
                var userDetails;
                var totalMessages;
                //var userDetails = this.attributes['userDetails'] 
                
                if (statusCode == 200)
                {
                    claimDetails = JSON.parse(myResult);
                    console.log("User Details = " + JSON.stringify(claimDetails));
                    totalMessages= Object.keys(claimDetails).length;
                    console.log("id" ,claimDetails[0]._id);
            
                          if (totalMessages == 0)
                          {
                              console.log("No claims associated with this person" );

                          }
                          else
                          { 
                              var claimDate = claimDetails[0].claimedDate; // The Policy start date got from the Database in UTC notation
                              var date = new Date( Date.parse(claimDate)); 
                              var date1 = date.toLocaleDateString();
            
                              var incidentDate = claimDetails[0].incidentDate; // The Policy start date got from the Database in UTC notation
                              var date = new Date( Date.parse(incidentDate)); 
                              var date2 = date.toLocaleDateString();
            
                              var defintion = "The claim number "+ claimDetails[0].claimNumber + "has been created for  " + claimDetails[0].claimReason  + "caused on " + date2  + " on " + date1 + " And the status of this claim is " + claimDetails[0].status ;
                              console.log("defintion" , defintion);
                          } 
                }
               else if(statusCode == 500)
          {
                          console.log("500");
                      }
                      else  
                      {
                          console.log("error")
                      }
            });
        }
        else { 
                console.log("In else part")	;
        }
    } else if(statusCode == 500){
        userDetails = JSON.parse(myResult);
        console.log("500Error" , userDetails);
        
    } else{
        console.log("Error" , userDetails);
    }
    		
});//http 


/*********************************
 * claimstatus 
 ********************************
function httpsGetClaims(myData, callback) {
    var options = {
            host: host_url,
            port: port_num,
            path: API_PATH +'/claims/mobile/'+ myData,
            method: 'GET',
            headers:{
                'Accept':'application/json',
                'Content-Type': 'application/json'
            },
        };
    var req = https.request(options, res => {
        res.setEncoding('utf8');
        var returnData = "";
  
        res.on('data', chunk => {
            returnData = returnData + chunk;
        });
  
        res.on('end', () => { // received the raw return data in the returnData variable.
        console.log("httpsGetClaims::returnData:claimsdetails " + JSON.stringify(returnData));
        callback(returnData,res.statusCode);  // this will execute whatever function the caller defined, with one argument
        });
    });
    req.end();
  }

在上面的代码中HttpClaims是回调函数,在其中我使用了if else循环,并且在该循环中我必须调用另一个回调函数httpGetClaims。

0 个答案:

没有答案