1

我正在尝试从博览会相机中保存图片。之后我会把这张照片发给cloudinary。因此我需要base64属性。但我有一个问题。

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)
  };

当我尝试保存图片时,出现以下错误:

可能的未处理承诺拒绝(id:0):

TypeError:无法读取未定义的属性“uri”

我检查了另一个问题和答案。但我没有找到解决方案。我该如何解决?

4

1 回答 1

0

你可能想要更多这样的东西。

  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);
  };

在您在上面发布的示例中,您将返回 this.setState 和控制台日志的组合,此时您应该返回初始takePictureAsync承诺解决时的数据。

于 2018-12-18T18:46:17.593 回答