Javascript 退出从react native上载照片时出现未处理的错误

Javascript 退出从react native上载照片时出现未处理的错误,javascript,reactjs,react-native,error-handling,Javascript,Reactjs,React Native,Error Handling,这是我的组件,用于从用户手机中选择和上传照片。它的工作很好,当用户打开手机照片库但决定取消并没有选择任何照片上传时,我会得到如下错误: “YellowBox.js:82可能的未处理承诺拒绝(id:0): 错误:用户已取消图像选择“ 我不知道在哪里以及如何捕捉和处理错误,有什么帮助吗 class ProfilePictureHandeler extends Component { constructor (props) { super(props) this.state = { loading

这是我的组件,用于从用户手机中选择和上传照片。它的工作很好,当用户打开手机照片库但决定取消并没有选择任何照片上传时,我会得到如下错误:

“YellowBox.js:82可能的未处理承诺拒绝(id:0): 错误:用户已取消图像选择“

我不知道在哪里以及如何捕捉和处理错误,有什么帮助吗

class ProfilePictureHandeler extends Component {
constructor (props) {
super(props)
this.state = {
  loading: false,
  dp: null
 }
}

openPicker () {
this.setState({ loading: true })
const Blob = RNFetchBlob.polyfill.Blob
const fs = RNFetchBlob.fs
window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest
window.Blob = Blob

const { currentUser } = firebase.auth()

ImagePicker.openPicker({
  cropping: true,
  height: 265,
  width: 265,
  mediaType: 'photo'
}).then(image => {
  const imagePath = image.path

  let uploadBlob = null

  const imageRef = firebase.storage().ref(`/users/${currentUser.uid}/profile`).child('dp.jpg')

  let mime = 'image/jpg'
  fs.readFile(imagePath, 'base64')
        .then((data) => {
          return Blob.build(data, { type: `${mime};BASE64` })
        })
      .then((blob) => {
        uploadBlob = blob
        return imageRef.put(blob, { contentType: mime })
      })
       .then(() => {
         uploadBlob.close()
         return imageRef.getDownloadURL()
       })
       .then((url) => {
         firebase.database().ref(`/users/${currentUser.uid}/profile`).update({
           profile_picture: url
         })
         let obj = {}
         obj['loading'] = false
         obj['dp'] = url
         this.setState(obj)
       })
       .catch((error) => {
         console.log(error + 'OPEN PICKER AGAIN')
       })
       .catch((error) => {
         console.log(error)
       })
       .catch((error) => {
         console.log(error)
       })
})
 }

render () {
const selectedPicture = this.state.dp ? (<TouchableOpacity onPress={() => this.openPicker()}>
  <Avatar
    height={265}
    rounded
    source={{ uri: this.state.dp }}
    activeOpacity={0.7}
    />
  </TouchableOpacity>) : (

  <TouchableHighlight onPress={() => this.openPicker()} >

    <Avatar
      rounded
      height={265}
      source={require('../../src/assets/man.png')}
      activeOpacity={0.7}
 />
  </TouchableHighlight>

 )
    // Default picture, d
const standardPicture = this.state.loading ? <ActivityIndicator animating={this.state.loading} /> : (
  <View>
    {selectedPicture}
  </View>
)

return (
  <TouchableHighlight onPress={() => this.openPicker()} >
    <View>
      {standardPicture}
    </View>
  </TouchableHighlight>

 )
 }
}

export default ProfilePictureHandeler
class ProfilePictureHandler扩展组件{
建造师(道具){
超级(道具)
此.state={
加载:false,
dp:null
}
}
openPicker(){
this.setState({loading:true})
const Blob=RNFetchBlob.polyfill.Blob
常量fs=RNFetchBlob.fs
window.XMLHttpRequest=RNFetchBlob.polyfill.XMLHttpRequest
window.Blob=Blob
const{currentUser}=firebase.auth()
ImagePicker.openPicker({
裁剪:没错,
身高:265,
宽度:265,
媒体类型:“照片”
})。然后(图像=>{
常量imagePath=image.path
让uploadBlob=null
const imageRef=firebase.storage().ref(`/users/${currentUser.uid}/profile`).child('dp.jpg'))
让mime='image/jpg'
fs.readFile(imagePath“base64”)
。然后((数据)=>{
返回Blob.build(数据,{type:`${mime};BASE64`})
})
.然后((blob)=>{
uploadBlob=blob
返回imageRef.put(blob,{contentType:mime})
})
.然后(()=>{
uploadBlob.close()文件
返回imageRef.getDownloadURL()
})
。然后((url)=>{
firebase.database().ref(`/users/${currentUser.uid}/profile`)。更新({
个人资料图片:url
})
设obj={}
obj['loading']=false
obj['dp']=url
此设置状态(obj)
})
.catch((错误)=>{
console.log(错误+“再次打开选择器”)
})
.catch((错误)=>{
console.log(错误)
})
.catch((错误)=>{
console.log(错误)
})
})
}
渲染(){
const selectedPicture=this.state.dp?(this.openPicker()}>
) : (
this.openPicker()}>
)
//默认图片,d
const standardPicture=this.state.loading?:(
{selectedPicture}
)
返回(
this.openPicker()}>
{standardPicture}
)
}
}
导出默认配置文件PictureHandler

ImagePicker。如果用户取消选择图像,openPicker()将抛出错误

尝试向您的代码中添加一个
catch
,如下所示:

ImagePicker.openPicker(options)
.then(image => {
    // do your stuff
})
.catch(error => {
    // add this to your code
});

ImagePicker.openPicker()
如果用户取消选择图像,将抛出错误

尝试向您的代码中添加一个
catch
,如下所示:

ImagePicker.openPicker(options)
.then(image => {
    // do your stuff
})
.catch(error => {
    // add this to your code
});

以下是一个对我有效的解决方案:

ImagePicker.openPicker({
宽度:300,
身高:400,
裁剪:没错,
})
。然后(图像=>{
/*
图像={
“cropRect”:{“高度”:zzz,“宽度”:sss,“x”:aaa,“y”:qqq},
“高度”:xxx,
“mime”:“图像/jpeg”,
“修改日期”:“1611843171000”,
“路径”:file:///storage/emulated/0/Android/data/.../.....jpg",
“大小”:59579,
“宽度”:yyy
}
*/
})
.catch(错误=>{
如果(error.code=='E\u PICKER\u CANCELLED'){//这里是解决方案
返回false;
}
});

以下是一个对我有效的修复方法:

ImagePicker.openPicker({
宽度:300,
身高:400,
裁剪:没错,
})
。然后(图像=>{
/*
图像={
“cropRect”:{“高度”:zzz,“宽度”:sss,“x”:aaa,“y”:qqq},
“高度”:xxx,
“mime”:“图像/jpeg”,
“修改日期”:“1611843171000”,
“路径”:file:///storage/emulated/0/Android/data/.../.....jpg",
“大小”:59579,
“宽度”:yyy
}
*/
})
.catch(错误=>{
如果(error.code=='E\u PICKER\u CANCELLED'){//这里是解决方案
返回false;
}
});

你可以创建一个小提琴吗?你可以创建一个小提琴吗?谢谢!你对汉德尔的问题有什么线索吗?我想“this.setState({loading:false})”。。像callback(this.setState({loading:false}))一样,如果我知道“callback”包含什么数据,我就可以。您可以添加
this.setState({loading:false})
来代替console.log,我建议您使用从“回调”中获得的消息发送一条toast消息。如果此回答解决了您的问题,那么如果您能接受我的回答来结束此问题,我将不胜感激。谢谢你:)啊,谢谢你!你对汉德尔的问题有什么线索吗?我想“this.setState({loading:false})”。。像callback(this.setState({loading:false}))一样,如果我知道“callback”包含什么数据,我就可以。您可以添加
this.setState({loading:false})
来代替console.log,我建议您使用从“回调”中获得的消息发送一条toast消息。如果此回答解决了您的问题,那么如果您能接受我的回答来结束此问题,我将不胜感激。谢谢:)