我正在使用 coingecko API 开发一个简单的加密价格跟踪器。我当前的代码不会从 API 链接访问任何 JSON 对象,并且我没有收到表明问题所在的错误或警告:https ://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=比特币%2C%20ethereum%2C%20ripple&order=market_cap_desc&per_page=100&page=1&sparkline=false
但是,出于测试目的,如果我使用来自 jsonplaceholder 的这个 API 链接,它绝对可以正常工作:https ://jsonplaceholder.typicode.com/users
这是我的代码:
export const getStaticPaths = async () => {
//Does not work with this json url
// const res = await fetch('https://jsonplaceholder.typicode.com/users')
const res = await fetch('https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=bitcoin%2C%20ethereum%2C%20ripple&order=market_cap_desc&per_page=100&page=1&sparkline=false')
const data = await res.json();
const paths = data.map(coin => {
return {
params: {id: coin.id}
}
})
return {
paths,
fallback: false
}
}
export const getStaticProps = async (context) => {
const id = context.params.id;
//Does not work with this json url
// const res = await fetch('https://jsonplaceholder.typicode.com/users/' + id )
const res = await fetch('https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=' + id + '%2C%20&order=market_cap_desc&per_page=100&page=1&sparkline=false/')
const data = await res.json();
console.log(data)
return {
props: {coin: data}
}
}
const Details = ({ coin }) => {
return(
<div>
<h1>Coin Page</h1>
<h2>{ coin.name }</h2>
<h2>{ coin.symbol }</h2>
</div>
)
}
export default Details;
正如我所说,我没有收到任何错误或警告,因此任何有关故障排除的帮助将不胜感激,谢谢