React Native(Android)可能未处理的承诺拒绝错误

时间:2018-12-22 18:39:04

标签: reactjs react-native axios github-api expo

我正在尝试将发帖请求发送到google api以进行文本检测。但是首先我必须拍张照片,然后才能使用axios将这张照片发送给Google api。

以下代码用于拍照。

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

以下代码用于发送请求goole api。

sendVisionOCR(data){
      let body = {
        "requests":[
          {
            "image":{
              "content": data
            },
            "features": [
                  {
                    "type": "DOCUMENT_TEXT_DETECTION",
                    "maxResults": 1
                  }
                ]
          }
        ]
      }
axios.post('https://vision.googleapis.com/v1/images:annotate?key=xxXXxx', body)
      .then((response) => {
        console.log("worked", response)

      })
      .catch((err) => console.log("err", err))
}

运行代码时,出现这些错误;

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

错误:参数“ localUri”无效。它必须是字符串!

但是有时候,如果我更改代码的一小部分,就会看到无限循环。它正在尝试发送base64文件。

"axios": "^0.18.0",
"expo": "^31.0.2",
"react": "16.5.0",

我也尝试获取操作。它不起作用吗?

有什么问题?为什么axios无法正常工作?

2 个答案:

答案 0 :(得分:0)

我找到了解决方案。实际上,我的代码正在正确地工作。还有另一个问题。当我运行代码时,编码图片base64格式需要花费很长时间,并且有时似乎只能工作。有new个问题。

答案 1 :(得分:0)

在对插件代码进行更改之后,我遇到了同样的问题,并且可以正常工作。 在expo-local-authentication内部,只有一个文件:LocalAuthentication.js在该文件上进行了以下更改。

const result = await ExpoLocalAuthentication.authenticateAsync({ ...options, promptMessage });  

替换为

import { Platform } from 'react-native';
let result=undefined ; 
if(Platform.OS == 'ios'){
    result = await ExpoLocalAuthentication.authenticateAsync({ ...options, promptMessage });  
}else{
    result = await ExpoLocalAuthentication.authenticateAsync();
}

谢谢:)

相关问题