Using the initial promise "crypto.model.find()" allows me to store an array of "symbols" ( symbol[] ) from the database and retrieve some IDs that I will utilize to construct a URL for making a request to an API using axios.get(url).
Upon receiving a response from the API in the second promise, it appears that I lose access to my symbol[] array.
At this point, I require both the API data and the symbol[] array, but I am unsure about how to achieve this.
I have come across suggestions of returning an array and passing it along the chain of promises, however, in this particular scenario, it seems challenging to implement such an approach like so: return [axios.get(url), symbol[]]
// Retrieving coin descriptions from DB using Mongoose
cryptoModel.find()
.then(docsCrypto => {
let coingeckoIDsRequest = '';
let symbol = [];
// Iterating through the DB response
docsCrypto.forEach((eachCrypto) => {
// Building the symbol array based on DB values
symbol.push(eachCrypto.symbol)
// Creating a portion of the HTTPS API URL request with the DB IDs
coingeckoIDsRequest += eachCrypto.coingecko_id + '%2C'
})
// Constructing the URL
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)
})
// !!! >>>> My goal is to proceed to the next line with both the API data AND the symbol[] array
.then(res => console.log(res))
// Log error if any
.catch(err => console.log(err))