【问题标题】:Difficulty accessing specific data in axios GET requestaxios GET请求中访问特定数据有困难
【发布时间】:2018-01-21 03:33:58
【问题描述】:

我正在使用 CryptoCompare API (https://www.cryptocompare.com/api/#-api-data-coinlist-),但在搜索时无法访问特定的硬币信息。相反,我返回的是整个硬币列表,而不是我搜索的那个。我猜这是我当时的陈述中的一个问题,但我不确定。这是我到目前为止的代码 - 问题区域在 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: ""
        })
    }

【问题讨论】:

    标签: javascript reactjs get axios


    【解决方案1】:

    它在文档中说(据我了解)这个 get 请求不带任何参数。

    硬币列表

    获取网站上所有可用硬币的一般信息。

    网址参数

    因此,当您发送请求时,您将始终获得所有硬币。现在您可以做的是通过响应过滤您唯一感兴趣的内容。这意味着在.then 中使用的函数主体中,您可以简单地阅读通过param 提供的有关硬币的信息。

    【讨论】:

      猜你喜欢
      • 2018-02-01
      • 2020-06-20
      • 2017-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-06
      • 2021-06-15
      • 2020-02-14
      相关资源
      最近更新 更多