I take advantage of the first promise "crypto.model.find()" to store an array of "symbols" ( symbol[] ) from the DB and get some IDs I will use to create an URL to make a request to an API >>> axios.get(url)
In the second promise, I receive the answer from the API but I don't have acccess to my array symbol[].
I need both at this stage and I don't know how to do it.
I read about returning an array and pass it down the promises chain but in this case, I don't think I can use such an array : return [ axios.get(url), symbol[] ]
// getting coins description in my DB with Mongoose
cryptoModel.find()
.then ( docsCrypto => {
let coingeckoIDsRequest = '';
let symbol = [];
// loop on the response from the DB
docsCrypto.forEach( (eachCrypto) => {
// Filling the array of symbols from the DB
symbol.push( eachCrypto.symbol )
// creating a chunk of the HTTPS API URL request with the IDs from the DB
coingeckoIDsRequest += eachCrypto.coingecko_id + '%2C'
})
// URL creation
let url = 'https://api.coingecko.com/api/v3/coins/markets?vs_currency=eur&ids=' +
coingeckoIDsRequest.substr(0, coingeckoIDsRequest.length-3) +
'&order=market_cap_desc&per_page=100&page=1&sparkline=false&price_change_percentage=24h%2C7d%2C30d%2C200d'
// returning the API data
return axios.get(url)
})
// !!! >>>> I want to get to the next line with the data from the API AND the array "symbol[]"
.then (res => console.log(res))
// if error > console.log
.catch (err => console.log(err))