【问题标题】:react-native warning: Possible unhandled promise: can't find variable: listenersreact-native 警告:可能未处理的承诺:找不到变量:侦听器
【发布时间】:2016-12-20 21:00:03
【问题描述】:

我在运行我的 react-native 移动应用时收到此警告。 我尝试将带有 react-native-image-picker 的照片上传到 firebase(在存储中),并将这张照片的 url 保存在 firebase(数据库)中。上传成功,但是当程序尝试将 url 保存在数据库中时,应用程序会发出警告:“可能未处理的承诺拒绝:找不到变量:侦听器”。 以下是我的代码:

ImagePicker.showImagePicker(options, (response) => {

  let filePath = response.path;
  let fileName = this.props.imageName;
  let rnfbURI = RNFetchBlob.wrap(filePath);
  Blob
     .build(rnfbURI, { type : 'image/png;'})
     .then((blob) => {
  // upload image using Firebase SDK
        this.props.bdd.storage()
            .ref('images')
            .child(fileName)
            .put(blob, { contentType : 'image/jpg' })
            .then((snapshot) => {

                 console.log('Uploaded', snapshot.totalBytes, 'bytes.');
                 console.log(snapshot.metadata);
                 console.log('downlo='+snapshot.metadata.downloadURLs[0]);
                var ref = this.props.bdd.database().ref('promotions/'+ fileName);
                alert(snapshot.metadata.downloadURLs[0]);
                ref.update({
                  image_url:snapshot.metadata.downloadURLs[0]
                });
               }).catch(function(error) {
                console.error('Upload failed:', error);
                 });
     })   

});

}

【问题讨论】:

  • 什么是listeners

标签: reactjs react-native promise warnings


【解决方案1】:

您确实没有处理外部承诺(Blob.build one)拒绝。问题是如果这个操作失败、抛出等等,那么你将无法恢复,因为你根本不处理这种情况。

正确的方法是使用return this.props.bdd.storage() 返回内部承诺并将catch 块添加到外部块。像这样的:

Blob
  .build(rnfbURI, {
    type: 'image/png;'
  })
  .then((blob) => {
    // upload image using Firebase SDK
    return this.props.bdd.storage()
      .ref('images')
      .child(fileName)
      .put(blob, {
        contentType: 'image/jpg'
      })
      .then((snapshot) => {

        console.log('Uploaded', snapshot.totalBytes, 'bytes.');
        console.log(snapshot.metadata);
        console.log('downlo=' + snapshot.metadata.downloadURLs[0]);
        var ref = this.props.bdd.database().ref('promotions/' + fileName);
        alert(snapshot.metadata.downloadURLs[0]);
        ref.update({
          image_url: snapshot.metadata.downloadURLs[0]
        });
      })
      .catch(function(error) {
        console.error('Upload failed:', error);
        throw error;
      });
  })
  .catch(error => {
    console.log('One of the above operations failed', error)
  })

注意这部分:

.catch(function(error) {
  console.error('Upload failed:', error);
  throw error;
})

如果this.props.bdd.storage() 失败,您将进入此错误处理程序,然后通过重新throw'ing 错误使其进一步传播到外部catch 块。所以所有可能的拒绝都会被处理。

【讨论】:

    【解决方案2】:

    我认为是因为你没有做出完整的承诺,通常是这样的:

    yourPromise()
    .then((response) => {/*this callback will be executed if promise is completed*/}, 
          (cause) => {/*this callback will be executed if promise is rejected*/})
    .catch((err) => {/*this callback will be execute if some exception is thown*/});
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-15
      • 2020-07-21
      • 2017-12-20
      • 1970-01-01
      • 2020-04-28
      • 2021-04-08
      • 2020-01-01
      相关资源
      最近更新 更多