简体   繁体   English

找不到变量 atob React Native+Firebase 错误

[英]Can't find variable atob React Native+Firebase Error

I have this post screen where I can pick an image from the camera roll and type a text and I want it to be saved in Firebase .我有这个帖子屏幕,我可以从相机胶卷中选择一张图像并输入一个文本,我希望它保存在Firebase中。 Here is my code in fire.js这是我在fire.js中的代码

 addPost = async({text,localUri}) => {
    const remoteUri = await this.uploadPhotoAsync(localUri)

    return new Promise((res,rej) => {
        this.firestore.collection("posts").add({
            text,
            uid: this.uid,
            timestamp:this.timestamp,
            image: remoteUri
        })
        .then(ref => {
            res(ref)
        })
        .catch(error => {
            rej(error)
        })
    })
}

uploadPhotoAsync = async uri => {
    const path =  `photos/${this.uid}/${Date.now()}.jpg`

    return new Promise(async (res,rej) => {
        const response = await fetch(uri)
        const file = await response.blob()

        let upload = firebase.storage().ref(path).put(file)

        upload.on(firebase.storage.TaskEvent.STATE_CHANGED,snapshot => {},
        err => {
            rej(err)
        },
        async () => {
            const url = await upload.snapshot.ref.getDownloadURL()
            res(url)
        }
        )
    })
}

And here is my postscreen.js screen where I'm getting the error can't find variable atob ,这是我的postscreen.js屏幕,我收到错误can't find variable atob

please suggest me a solution.请建议我一个解决方案。

handlePost = () => {
 Fire.shared.addPost({text:this.state.text.trim(),
  localUri:this.state.image })
   .then(ref => {
    this.setState({text:"",image:undefined})
    this.props.navigation.goBack()
     }).catch(error => {
       alert(error)
     })
   }



 pickImage = async () => {
     let result = await ImagePicker.launchImageLibraryAsync({
       mediaTypes: ImagePicker.MediaTypeOptions.Images,
       allowsEditing:true,
       aspect:[4,3]
     })

 if(!result.cancelled) {
   this.setState({image: result.uri})
 }

} }

By the way, I can see the image is saved in Firestore storage but I can't see the text and photo in the Firestore database顺便说一句,我可以看到图像保存在Firestore存储中,但在Firestore数据库中看不到文字和照片

This is a bug in some versions of firebase .这是firebase某些版本中的错误。

A workaround is to import base64 in the app.js and define it in case it's not defined.一种解决方法是在app.js中导入 base64 并定义它以防它未定义。

import {decode, encode} from 'base-64'

if (!global.btoa) { global.btoa = encode }
if (!global.atob) { global.atob = decode }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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