0

I have simple code and its getting data from external api in nodejs . but am unable to get data from it . Here is the code

econst request = require('request');
const options = {
    url: api_url,
    method: 'GET',
    headers: {
        'Accept': 'application/json',
        'Accept-Charset': 'utf-8'
        
    }
};

request(options, function(err, res, body) {
    let json = JSON.parse(body);
  //  console.log(json.foods[0]);
     APIDATA = json.foods[0];

     
}
 yetone = APIDATA;
);

how can u get this apidata outside it and can use it somewhere else

Syed Murtaza
  • 31
  • 1
  • 4

1 Answers1

-1

You need to return the APIDATA and call the function that makes api call in whereever you want to use the APIDATA.

first of all you should not use the request module.

export const request = async () => {
  const response = await fetch(requestUrl, options);
  const parsedResponse = JSON.parse(response);
  return parsedResponse;
}

somewhere in code you need to use data:

import {request} from 'path to file';
const apiResponse = request();

for more help check out here

mscandan
  • 37
  • 1
  • 4