Click here to Skip to main content
15,886,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
EDIT : it's ok now !!!


Hello to all, 

I would like to know how it is possible to centralize on a single api the data of several api 

I would like to have the result of the four res.data at the address /coingeckotest

Currently I can see in the terminal the result I want to have but on the page /coingeckotest I have a return of "null" value 

Thank you in advance for your answers

What I have tried:

<pre>async function datauniswap() {
let datauniswap = await axios
.get('https://api.coingecko.com/api/v3/exchanges/uniswap/tickers')
.then((res) => {
console.log(res.data)
})
return datauniswap
}
 
async function datasushiswap() {
let datasushiswap = await axios
.get('https://api.coingecko.com/api/v3/exchanges/sushiswap/tickers')
.then((res) => {
console.log(res.data)
})
return datasushiswap
}
 
async function datacurvefinance() {
let datacurvefinance = await axios
.get('https://api.coingecko.com/api/v3/exchanges/curve/tickers')
.then((res) => {
console.log(res.data)
})
return datacurvefinance
}
 
async function dataquickswap() {
let dataquickswap = await axios
.get('https://api.coingecko.com/api/v3/exchanges/quickswap/tickers')
.then((res) => {
console.log(res.data)
})
return dataquickswap
}
 
 
 
 
server.get('/coingeckotest', async (req, res) => {
exchangeone = await datauniswap();
exchangetwo = await datasushiswap();
exchangethree = await datacurvefinance();
exchangequattro = await dataquickswap();
cacheTime = Date.now();
res.json[datauniswap, datasushiswap, datauniswap, datacurvefinance]
})
Posted
Updated 21-Jul-22 7:34am
v2

1 solution

Quote:
JavaScript
res.json[datauniswap, datasushiswap, datauniswap, datacurvefinance]
As far as I can see, res.json is a function. You need to call that function, passing in the data you want to return.

You also need to send the variables containing the data you've loaded, not the functions used to load the data.

For example:
JavaScript
res.json({ uniswap: exchangeone, sushiswap: exchangetwo, curvefinance: exchangethree, quickswap: exchangequattro });

Once you've fixed that, you'll probably want to fix your code to issue the external requests in parallel, rather than sequentially:
JavaScript
server.get('/coingeckotest', async (req, res) => {
    // Start the external calls in parallel:
    const promises = [
        datauniswap(),
        datasushiswap(),
        datacurvefinance(),
        dataquickswap()
    ];
    
    // Wait for them all to finish:
    const uniswap: await promises[0],
          sushiswap: await promises[1],
          curvefinance = await promises[2],
          quickswap = await promises[3];
    
    // Return the data:
    cacheTime = Date.now();
    res.json({ uniswap, sushiswap, curvefinance, quickswap });
});
 
Share this answer
 
Comments
Member 15709071 19-Jul-22 9:26am    
Hello,
Thank you for your help however, the data from the Apis are still not displayed at the address '/coingeckotest'.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900