繁体   English   中英

在axios GET请求中难以访问特定数据

[英]Difficulty accessing specific data in axios GET request

我正在使用CryptoCompare API( https://www.cryptocompare.com/api/#-api-data-coinlist- ),并且在访问搜索时难以访问特定的硬币信息。 相反,我返回了整个硬币列表,而不是我搜索过的硬币列表。 我猜这是我的then语句中的问题,但我不确定。 这是我到目前为止的代码-问题区域在searchImage中

 class SearchForm extends React.Component{ constructor(){ super(); this.state = { currentSearch: "", priceResults: [], cryImage: [], } this.handleChange = this.handleChange.bind(this); this.handleSubmit = this.handleSubmit.bind(this); this.cryPrice = this.cryPrice.bind(this); } cryPrice(param){ axios.get(`https://min-api.cryptocompare.com/data/pricemulti?fsyms=${param}&tsyms=CAD`) .then(res => { const cryptos = res.data; console.log(cryptos); this.setState({ priceResults: cryptos }) }) } searchImage(param){ axios.get(`https://min-api.cryptocompare.com/data/all/coinlist`, { params: { name: `${param}`, } }).then(res => { const cryImage = res.data; console.log(cryImage); this.setState({ cryImage }) }) } handleChange(e){ const currentSearchValue = e.target.value; this.setState({ currentSearch: currentSearchValue }) console.log(currentSearchValue); } handleSubmit(e){ e.preventDefault(); this.cryPrice(this.state.currentSearch); this.searchImage(this.state.currentSearch); this.setState({ currentSearch: "" }) } 

在文档中(据我了解),该get请求不包含任何参数。

硬币清单

获取网站上所有可用硬币的常规信息。

URL参数

没有

因此,当您发送请求时,您将始终获得所有硬币。 现在,您可以做的是通过响应筛选出唯一感兴趣的人。 这意味着,在您正在使用的函数的主体中.then那么您可以简单地读取有关通过param给出的硬币的信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM