1

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?

Ebru Gulec
  • 653
  • 2
  • 7
  • 16

1 Answers1

0

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.

jasonmerino
  • 3,220
  • 1
  • 21
  • 38