首页 > 解决方案 > 节点 Discord.js Axios API 索引

问题描述

我想通过存储在 json 文件中的项目列表进行索引,并调用每个 API 并带回数据。下面的代码显示了通过构建 API 链接来工作的索引/映射,但是如何让整个 API 调用和消息在索引内,以便每个列表项都由 API 调用和返回:

// {"342671641006047252":["MSFT","AMZN","CVNA","TEAM"]}
console.log(list);

// This is indexing through the list and bulding the link
const tickers = list
  .map((ticker, index) => `https://financialmodelingprep.com/api/v3/quote/${ticker}?apikey=6c7ee1f3c7a666228979fa0678fa22a3`)
  return message.channel.send(tickers)
  
// This is going to the api for list[0]
axios.get('https://financialmodelingprep.com/api/v3/quote/'+list[0]+'?apikey=6c7ee1f3c7a666228979fa0678fa22a3').then(resp => {
console.log(resp.data);
let symbol = resp.data[0].symbol;
let price = resp.data[0].price;
let changesPercentage = resp.data[0].changesPercentage;
return message.channel.send({embed: {
    color: 8311585,
    fields: [{
        name: "Ticker",
        value: `${symbol}`,
        inline: "true"
      },
      {
        name: "Price",
        value: `${price}`,
        inline: "true"
      },
      {
        name: "Change %",
        value: `${changesPercentage}`,
        inline: "true"
      },

标签: node.jsapiindexingdiscord.js

解决方案


没关系这个!我使用 forEach 而不是 .map 并让它工作!


推荐阅读