简体   繁体   中英

Centralize data from multiple APIs

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

Thanks in advance for your answers

this is my script

 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[exchangeone, exchangetwo, exchangethree, exchangequattro]
})
  1. Use axois in right way (use promises or async/await). Axois Examples

Solution:

async function datauniswap() {
  // can be configured via axios config baseUrl:
  // https://axios-http.com/docs/config_defaults
  let url = 'https://api.coingecko.com/api/v3/exchanges/uniswap/tickers'
  
  // TODO: handle errors via try/catch
  let response = await axios.get(url);

  // TODO: handle when data is undefined
  return response?.data
}
  1. This line is incorrect:
res.json[exchangeone, exchangetwo, exchangethree, exchangequattro]

If you are using express.js for the server- .json - is a function, you can try this:

Solution:

let data = [exchangeone, exchangetwo, exchangethree, exchangequattro]
res.json(data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM