【发布时间】: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