Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 无法获得任何费率';s值_Json_Reactjs_Api_Load - Fatal编程技术网

Json 无法获得任何费率';s值

Json 无法获得任何费率';s值,json,reactjs,api,load,Json,Reactjs,Api,Load,我正在尝试从exchangeratesapi加载数据,但我无法加载exchangerates的数据 componentDidMount() { this.setState({loading: true}) fetch("https://api.exchangeratesapi.io/latest") .then(response => response.json()) .then(data => {

我正在尝试从exchangeratesapi加载数据,但我无法加载exchangerates的数据

componentDidMount() {
        this.setState({loading: true})
        fetch("https://api.exchangeratesapi.io/latest")
            .then(response => response.json())
            .then(data => {
                console.log(data)
                this.setState({
                    loading: false,
                    currency: data,
            })
            })
    }

渲染的内部

var text = this.state.loading ? "loading..." : this.state.currency.bulgaria

但我相信有更好的方法可以做到这一点。

您正试图直接从
货币
访问该属性,但它存在于
费率中

这是不正确的:

{this.state.currency.RON}
应该是:

{this.state.currency.rates.RON}
同样,您创建的变量文本不会在任何地方使用。我想应该是这样的:

    render() {
            const {loading, currency} = this.state;
            console.log(currency); //only for debugging
            return (
                { loading? 'Loading...' : (
                <div>
                    <p>{currency.rates.RON}</p>
                </div>)
                }
            )
        }
render(){
const{loading,currency}=this.state;
console.log(货币);//仅用于调试
返回(
{正在加载?'正在加载…':(
{currency.rates.RON}

) } ) }
我尝试了
{this.state.currency.rates.RON}
但根本不起作用,它给了我“TypeError:无法读取未定义的属性'RON'”@Tingyunalu!你能控制一下currency的值,看看它是否有服务响应吗。检查更新的答案。
{this.state.currency.rates.RON}
    render() {
            const {loading, currency} = this.state;
            console.log(currency); //only for debugging
            return (
                { loading? 'Loading...' : (
                <div>
                    <p>{currency.rates.RON}</p>
                </div>)
                }
            )
        }