I am working on a bot that retrieves data from an API. I originally was developing the bot in browser-side Javascript, but I am now transitioning to Node.js. I am having trouble with my async function. I want to wait for a response from the API to create an object and work with the data; however, the code continues, and I received an 'undefined error' for my obj variable.
async function doGetRequest(URL) {
const axios = require('axios');
let res = await axios.get(URL);
let data = res.data;
console.log(data);
}
function readBids(pairs) {
let i = 0;
let TrianglePair = {pair: [], price: [], typeTrade: "", tradeAmount: 0, highestProfit: ""};
//var URL = "https://api.kucoin.com/api/v1/market/orderbook/level2_20?symbol=";
let tempVal;
for(i = 0; i < pairs.length; i ++) {
let obj = doGetRequest('https://api.kucoin.com/api/v1/market/orderbook/level2_20?symbol=SHIB-USDT');
console.log(obj);
tempVal = obj.bids[i][0];
TrianglePair.pair[i] = pairs[i];
TrianglePair.price[i] = tempVal;
console.log(response);
return TrianglePair
}
}
How can I wait for the API's response before continuing? Do I need to restructure the architecture of my code or is there another solution? Thanks!