Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
168 views
in Technique[技术] by (71.8m points)

javascript - Need to call Two APIs In Loop using node js

I have an array of ssn number and I have two api list in which I need to pass ssn number as request json so I need to call both api inside ssn loop so I pass ssn to json request during call both api but code is not work properly both api call at a time simulteniously, Where I need to call both api one by one.

Both API details and code are as follow

My Code:

let ssn = [460458524, 637625452, 453311896, 635285187, 455791630, 642348377, 463590491, 450730278, 641201851, 379965491];
async function getCRCDetails() {
  ssn.forEach(function (item) {
    if(item){
    let CBCOptions = {
      'method': 'POST',
      'url': 'https://loanboard.houstondirectauto.com/api/Report',
      'headers': {
        'Content-Type': 'application/json',
        'Cookie': 'ci_session=udmojmlc5tfl3epbrmtvgu6nao2f031p'
      },
      body: JSON.stringify({
        "token": loantoken,
        "action": "CBCReport",
        "variables": {
          ssn: item
        }
      })
    }
 request(CBCOptions, function (error, response) {
        console.log(item);
        console.log("CBCOPtion ", CBCOptions);
        if (error) throw new Error(error);
        result = (JSON.parse(response.body));
        console.log("Result =", result);
        CRCReport.push(result);
      })
 let EmployerInfoOptions = {
        'method': 'POST',
        'url': 'https://loanboard.houstondirectauto.com/api/Report',
        'headers': {
          'Content-Type': 'application/json',
          'Cookie': 'ci_session=udmojmlc5tfl3epbrmtvgu6nao2f031p'
        },
        body: JSON.stringify({
          "token": loantoken,
          "action": "getEmployerInfo",
          "variables": {
            ssn: item
          }
        })
      }
 request(EmployerInfoOptions, function (error, response) {
       console.log(response.body);

      })

}

Here I need to call API request one by one.Anyone Guide me please.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I prefer use async await method for this situation you need install and require async and request-promise after that :

const request = require("request-promise");
const async = require("async");
let ssn = [460458524, 637625452, 453311896, 635285187, 455791630, 642348377, 463590491, 450730278, 641201851, 379965491];

async function getCRCDetails() {
  //like a forEache
  async.eachSeries(ssn, async (item) => {
      let CBCOptions = {
        method: "POST",
        url: "https://loanboard.houstondirectauto.com/api/Report",
        headers: {
          "Content-Type": "application/json",
          Cookie: "ci_session=udmojmlc5tfl3epbrmtvgu6nao2f031p",
        },
        body: JSON.stringify({
          token: loantoken,
          action: "CBCReport",
          variables: {
            ssn: item,
          },
        }),
      };
      let EmployerInfoOptions = {
        method: "POST",
        url: "https://loanboard.houstondirectauto.com/api/Report",
        headers: {
          "Content-Type": "application/json",
          Cookie: "ci_session=udmojmlc5tfl3epbrmtvgu6nao2f031p",
        },
        body: JSON.stringify({
          token: loantoken,
          action: "getEmployerInfo",
          variables: {
            ssn: item,
          },
        }),
      };
      try {
        let resultCBCOptions = await request(CBCOptions);
        let EmployerInfoOptions = await request(EmployerInfoOptions);
        console.log(resultCBCOptions)
        console.log(EmployerInfoOptions)
        //do pushing resultCBCOptions
        //do pushing EmployerInfoOptions
      } catch (error) {
        console.log(error);
      }
    },
    () => {
      console.log("finished");
    }
  );
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...