简体   繁体   中英

React Native Possible Unhandled Promise Rejection (id: 0):

I'm trying the save picture from expo camera. After that i will send this picture to cloudinary. Therefore i need base64 properties. But i have a problem.

takePictureAndCreateAlbum = async () => {
    const { uri } = await this.camera.takePictureAsync({
      base64: true
    }).then(data => {
      return (
        this.setState({
          data
        }),
        console.log("data", data)
      )
    }).catch(err => {
      throw error;}
      )
      const asset = await MediaLibrary.createAssetAsync(uri);
      MediaLibrary.createAlbumAsync('Expo', asset)
        .then(() => {
          Alert.alert('Album created!')
        })
        .catch(error => {
          Alert.alert('An Error Occurred!')
        });

        this.sendCloudinary(this.state.data)
  };

When I tried to save a picture, I got these errors:

Possible Unhandled Promise Rejection (id: 0):

TypeError: Cannot read property 'uri' of undefined

I checked another question and answers. But i didn't find a solution. How can i fix?

You probably want something more like this.

  takePictureAndCreateAlbum = async () => {
    const { uri } = await this.camera
      .takePictureAsync({
        base64: true
      })
      .then(data => {
        this.setState({
          data
        }),
          console.log("data", data);
        return data;
      })
      .catch(err => {
        throw error;
      });
    const asset = await MediaLibrary.createAssetAsync(uri);
    MediaLibrary.createAlbumAsync("Expo", asset)
      .then(() => {
        Alert.alert("Album created!");
      })
      .catch(error => {
        Alert.alert("An Error Occurred!");
      });

    this.sendCloudinary(this.state.data);
  };

In the example you posted above you're returning a combination of this.setState and a console log, when you should be returning the data from when the initial takePictureAsync promise resolved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM