这是 CoinGecko API 返回的部分代码。
{
"id": "bitcoin",
"symbol": "btc",
"name": "Bitcoin",
},
"image": {
"thumb": "https://assets.coingecko.com/coins/images/1/thumb/bitcoin.png?1547033579",
"small": "https://assets.coingecko.com/coins/images/1/small/bitcoin.png?1547033579"
},
"market_data": {
"current_price": {
"aed": 26429.239288693556,
"ars": 430563.0457102599,
"aud": 10256.814195551682,
"bch": 35.13945542496315,
"bdt": 610699.0910671571,
"bhd": 2712.9039956563156,
"bmd": 7195.153895430029,
"bnb": 524.566548150439,
"brl": 28924.295609858014,
"btc": 1
使用 Reactjs,我试图在“市场数据”和“current_price”下访问比特币的加拿大价格。我可以成功访问“id”,但无法访问“image”或“market_data”下的任何内容。我不确定我是否使用了正确的符号。这是不成功的代码。
<h1> {data.id} </h1>
<h1> {data.symbol} </h1>
<h1> {data.market_data.current_price.cad} </h1>
这些是从控制台返回的 2 个错误。
未捕获的类型错误:无法读取未定义的属性“current_price”
The above error occurred in the <StockCrypto> component:
at StockCrypto (http://localhost:3000/react-CryptoMarket/static/js/main.chunk.js:1332:81)
at div
at CryptoStockPage
at Route (http://localhost:3000/react-CryptoMarket/static/js/vendors~main.chunk.js:59298:29)
at Switch (http://localhost:3000/react-CryptoMarket/static/js/vendors~main.chunk.js:59500:29)
at Router (http://localhost:3000/react-CryptoMarket/static/js/vendors~main.chunk.js:58933:30)
at BrowserRouter (http://localhost:3000/react-CryptoMarket/static/js/vendors~main.chunk.js:58553:35)
at div
at App
Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://reactjs.org/link/error-boundaries to learn more about error boundaries.