*可能的未处理承诺拒绝(id:0):类型错误:未定义不是对象(评估'result.cancelled')云图像上传

Posted

技术标签:

【中文标题】*可能的未处理承诺拒绝(id:0):类型错误:未定义不是对象(评估\'result.cancelled\')云图像上传【英文标题】:*Possible Unhandled Promise Rejection (id: 0): Type Error: undefined is not an object (eveluating 'result.cancelled') Cloudinary image upload*可能的未处理承诺拒绝(id:0):类型错误:未定义不是对象(评估'result.cancelled')云图像上传 【发布时间】:2020-07-10 09:40:49 【问题描述】:

谁能帮帮我。我有这个项目,我必须将图像上传到 cloudinary。这里的问题是关于Possible Unhandled Promise Rejection (id: 0): Type Error: undefined is not an object (eveluating 'result.cancelled')的警告有人能帮帮我吗?

这是我的构造函数

    constructor(props) 
  super(props);
this.state = 
  image: '',
  receiptamount: '',
  driverusername: '',
  requesterusername: '', 
  avatarSource: '',
;

这是我的功能

    pickImage = async () => 
      let result = await ImagePicker.showImagePicker(options, (response) => 
        console.log('Response = ', response);
    
        if (response.didCancel) 
          console.log('User cancelled image picker');
         else if (response.error) 
          console.log('ImagePicker Error: ', response.error);
         else if (response.customButton) 
          console.log('User tapped custom button: ', response.customButton);
         else 
          const source =  uri: 'data:image/jpeg;base64,' + response.data 
          
          this.setState(
            image: 

response.uri,
      );
      console.log( uri: response.uri);
      // You can also display the image using data:
      // const source =  uri: 'data:image/jpeg;base64,' + response.data ;
  
      this.setState(
        avatarSource: source,
      );
    
  );

    if (!result.cancelled) 
        
    
        // url generation via cloudinary
        let base64Img = this.state.avatarSource;
    
        //Add your cloud name
        let apiUrl = 'https://api.cloudinary.com/v1_1/'someinfo'/image/upload';
    
        let data = 
          file: base64Img,
          upload_preset: 'my_upload_preset',
          cloud_name : 'kusinahanglan',
        ;
        
        fetch(apiUrl, 
          method: 'POST',
          
      body: JSON.stringify(data),
      headers: 
        'content-type': 'multipart/form-data',
      ,
      
    ).then(r => 
      data = r._bodyText;
      console.log('data value:' + data)
      // uploads url to image as generated from cloudinary
      firebase.database().ref('receipts/' + this.state.requesterusername).set(
        imagename: JSON.parse(r._bodyText).public_id + "." + JSON.parse(r._bodyText).format,
        imageurl: JSON.parse(r._bodyText).secure_url,
        receiptamount: this.state.receiptamount,
        driverusername: this.state.driverusername,
        requesterusername: this.state.requesterusername,
        
        );
    );
  
;



render() 
  return (
    <View style=styles.container>
    
                            <TextInput
        value1=this.state.receiptamount
        placeholder = "receipt amount"
        onChangeText = (value1) => this.setState(receiptamount:value1)
        />
      
                                    <TextInput
        value2=this.state.driverusername
        placeholder = "driver username"
        onChangeText = (value2) => this.setState(driverusername:value2)
        />
        
                                      <TextInput
        value3=this.state.requesterusername
        placeholder = "requester username"
        onChangeText = (value3) => this.setState(requesterusername:value3)
        />
    
      <TouchableOpacity
        onPress=() => this.pickImage()
        style= width: 200, alignSelf: 'center' >
        <View style= backgroundColor: 'transparent' >
          this.state.image
            ? <Image
                source= uri: this.state.image 
                style=
                  width: 200,
                  height: 200,
                  borderRadius: 100,
                  alignSelf: 'center',
                
              />
            : <View
                style=
                  backgroundColor: 'grey',
                  width: 200,
                  height: 200,
                  borderRadius: 100,
                
              />
            
        
        </View>
      </TouchableOpacity>
    </View>
  );



const styles = StyleSheet.create(
container: 
  flex: 1,
  alignItems: 'center',
  justifyContent: 'center',

  backgroundColor: '#ecf0f1',
,
);

【问题讨论】:

【参考方案1】:

ImagePicker 的结果将是未定义的,因为该库不会在那里返回任何内容。将您的代码放在 response.didCancel 中。

let result = await ImagePicker.showImagePicker(options, (response) => 
    console.log('Response = ', response);

    if (response.didCancel) 
      console.log('User cancelled image picker');

    // url generation via cloudinary
    let base64Img = this.state.avatarSource;

    //Add your cloud name
    let apiUrl = 'https://api.cloudinary.com/v1_1/'my info'/image/upload';

    let data = 
      file: base64Img,
      upload_preset: 'my_upload_preset',
      cloud_name : 'kusinahanglan',
    ;

    fetch(apiUrl, 
      method: 'POST',
      body: JSON.stringify(data),
      headers: 
        'content-type': 'multipart/form-data',
      
    ).then(r => 
      data = r._bodyText;
      console.log('data value:' + data)
      // uploads url to image as generated from cloudinary
      firebase.database().ref('receipts/' + this.state.requesterusername).set(
    imagename: JSON.parse(r._bodyText).public_id + "." + JSON.parse(r._bodyText).format,
    imageurl: JSON.parse(r._bodyText).secure_url,
    receiptamount: this.state.receiptamount,
    driverusername: this.state.driverusername,
    requesterusername: this.state.requesterusername,

    );
);
     else if (response.error) 
      console.log('ImagePicker Error: ', response.error);
     else if (response.customButton) 
      console.log('User tapped custom button: ', response.customButton);
     else 
      const source =  uri: 'data:image/jpeg;base64,' + response.data 

      this.setState(
        image: response.uri,
  );
  console.log( uri: response.uri);
  // You can also display the image using data:
  // const source =  uri: 'data:image/jpeg;base64,' + response.data ;

  this.setState(
    avatarSource: source,
  );

);

【讨论】:

以上是关于*可能的未处理承诺拒绝(id:0):类型错误:未定义不是对象(评估'result.cancelled')云图像上传的主要内容,如果未能解决你的问题,请参考以下文章

可能的未处理承诺拒绝(id:0):错误:权限被拒绝(通过“CameraRoll.saveToCameraRoll()”保存图像时)

可能的未处理承诺拒绝(id:0):TypeError:适配器不是函数。 (在“适配器(配置)”中,“适配器”未定义)?

Axios 承诺处理 - 在 react-native 中获取“可能的未处理承诺拒绝 - 类型错误:网络请求失败”

如何修复可能的未处理承诺拒绝(id:0)?以及如何修复无法读取未定义的属性“导航”?

可能的未处理承诺拒绝(id:1):TypeError:网络请求失败

无法读取未定义和未处理的承诺拒绝的属性“捕获”