【问题标题】:How do I access this response from the console.log?如何从 console.log 访问此响应?
【发布时间】:2018-06-18 18:13:14
【问题描述】:

通常,我使用fetch 更容易做到这一点,但这个包不使用fetch。 我阅读了与我类似的其他问题,这是我尝试这样做的尝试:

SelectPhoto = () =>{
ImagePicker.openPicker({
}).then((imgResponse) => {
  console.log(imgResponse.path); <-- Trying to access the path
});
}

我不断收到undefined,这是我的控制台日志结果:

0:{size: 745660, mime: "image/jpeg", height: 2960, width: 1440, path: "file:///storage/emulated/0/DCIM/Screenshots/Screenshot_20180617-173313.jpg"} length:1

谁能帮我解决这个问题?非常感谢!

【问题讨论】:

  • 虽然我不确定:1.) 使用imgResponse[0].path?或 2.) 首先保护路径不被读取?
  • 好的,这行得通。另外,我不这么认为,因为它告诉我可以从这个包中做到这一点:npmjs.com/package/react-native-customized-image-picker。现在我需要做的就是弄清楚如何将状态更改为该路径。 @SebastianRothbucher

标签: reactjs react-native console.log


【解决方案1】:

您似乎在使用react-native-customized-image-picker

根据他们的docs 和你的console.log 输出,你可以得到路径并将其设置为如下状态:

import React from 'react'
import ImagePicker from 'react-native-customized-image-picker'

class App extends React.Component {
  constructor (props) {
    super(props)

    this.state = { path: null }
    this.handleClick = this.handleClick.bind(this)
  }

  handleClick () {
    ImagePicker.openPicker({}).then(image => {
      const path = image[0].path

      this.setState({ path })
    })
  }

  render () {
    return <div onClick={this.handleClick}>Select image</div>
  }
}

【讨论】:

  • 非常感谢!
  • 还有一件事,我怎样才能得到这个包的文件名?
  • 如果库没有返回image对象中的filename,那么可以手动获取如下:stackoverflow.com/a/423385/4312466
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-12
  • 2021-12-10
  • 1970-01-01
  • 2010-09-20
  • 2018-06-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多