Why does fetch return a weird hash of integers - part 2?

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-25 04:30:25

问题


I'm using async/await with React Native.

My result from response.json() is: { _45: 0, _81: 0, _65: null, _54: null }

For whatever reason, the actual response I want is located in _65 and I have no idea what these random keys are.

It seems that it is related to the fact that .json() returns a Promise.

  componentDidMount() {
    this.getData().then(data => this.setState({ data }))
  }
  async getData() {
    try {
      let response = await fetch(myUrl)
      let json = await response.json()
      return json
    } catch(err) {
      Alert.alert(null, err)
    }
  }
  render() {
    const { data } = this.state
  ...

回答1:


The answer is: this.getData().then(data => this.setState({ data }))



来源:https://stackoverflow.com/questions/36285564/why-does-fetch-return-a-weird-hash-of-integers-part-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!