【发布时间】:2019-05-20 21:19:18
【问题描述】:
我利用第一个承诺“crypto.model.find()”从数据库中存储一个“符号”数组 (symbol[]) 并获取一些 ID,我将使用这些 ID 创建一个 URL 来发出请求到 API >>> axios.get(url)
在第二个承诺中,我从 API 收到了答案,但我无法访问我的数组符号 []。
现阶段我都需要,但我不知道该怎么做。
我读到了关于返回一个数组并将其传递到 Promise 链的内容,但在这种情况下,我认为我不能使用这样的数组: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))
【问题讨论】:
标签: javascript promise chaining