【问题标题】:axios is not getting any data from an apiaxios 没有从 api 获取任何数据
【发布时间】:2021-01-17 23:17:36
【问题描述】:

我的 axios 函数没有从 api 返回任何 json 数据。

这里是请求函数

  const getCoins = async () => {
      try {
          return await (await axios.get('https://api.coincap.io/v2/assets'));
      } catch (error) {
          console.log(error);
      }
  }

我在我的索引路由上调用这个函数

  app.get('/', async function (req, res) {
      res.render('index', {title:'Index',data:getCoins});
      console.log(getCoins);
  });

console.log 不记录任何数据,只记录整个函数:

async () => {
      try {
          return await JSON.stringify(axios.get('https://api.coincap.io/v2/assets')); 
      } catch (error) {
          console.log(error);
      }
  }

意味着我无法在我的索引页面上循环浏览它

   <tr>
   <% for( let index = 0; index < data.length; index++ ) { %>
    <td>data[i].id</td>
    <td>data[i].rank</td>
    <td>data[i].symbol</td>
    <td>data[i].priceUsd</td>
    <td>data[i].changePercent24Hr</td>
   <% } %>
   </tr>

我期望返回的数据是这样的

  "data": [
    {
      "id": "bitcoin",
      "rank": "1",
      "symbol": "BTC",
      "name": "Bitcoin",
      "supply": "17193925.0000000000000000",
      "maxSupply": "21000000.0000000000000000",
      "marketCapUsd": "119150835874.4699281625807300",
      "volumeUsd24Hr": "2927959461.1750323310959460",
      "priceUsd": "6929.8217756835584756",
      "changePercent24Hr": "-0.8101417214350335",
      "vwap24Hr": "7175.0663247679233209"
    },
],

【问题讨论】:

  • res.render('index', {title:'Index', ...(await getCoins()).data});

标签: javascript node.js express axios


【解决方案1】:

两个错误

  1. 从 getCoins 中删除一个 await
const getCoins = async () => {
    try {
        return await axios.get('https://api.coincap.io/v2/assets'));
    } catch (error) {
        return error
    }
}
  1. 您必须等待从异步函数获取数据,因此在 getCoins 之前添加等待。还要加()调用函数
app.get('/', async function (req, res) {
    res.render('index', {title:'Index',data:getCoins});
    console.log(await getCoins());
});

【讨论】:

    【解决方案2】:

    您必须添加括号才能打印函数的输出

    console.log(getCoins())

    【讨论】:

      【解决方案3】:

      有多种口味,但我这样做:

      app.get("/", async (req, res) => {
        try {
          let response = await getCoins();
          console.log("response", response.data);
          res.render("index.html"); // also attach data to handle by view engine, ...
        } catch (error) {
          console.error(error);
          // handle all errors here based on which type or which function returns error
          /// res.render() error page or suitable http status, ...
        }
      });
      
      let getCoins = () => axios.get("https://api.coincap.io/v2/assets");
      

      【讨论】:

        【解决方案4】:

        这不是您使用 async/await 的方式。您必须等待结果,将其放入变量中,然后使用它。试试看:

         const getCoins = async () => {
          try {
              const result = await axios.get('https://api.coincap.io/v2/assets');
              console.log(result);
          } catch (error) {
              console.log(error);
          }
        

        }

        【讨论】:

          猜你喜欢
          • 2021-09-22
          • 2019-06-27
          • 1970-01-01
          • 2021-09-05
          • 2023-01-12
          • 2020-12-17
          • 2023-02-22
          • 2020-10-08
          • 2018-01-31
          相关资源
          最近更新 更多