【问题标题】:Can't find variable atob React Native+Firebase Error找不到变量 atob React Native+Firebase 错误
【发布时间】:2020-06-16 14:29:39
【问题描述】:

我有这个帖子屏幕,我可以在其中从相机胶卷中选择一张图片并输入文字,我希望它保存在Firebase 中。 这是我在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)
        }
        )
    })
}

这是我的postscreen.js 屏幕,我收到错误can't find variable atob

请给我一个解决方案。

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

}

顺便说一句,我可以看到图片保存在Firestore存储中,但我看不到Firestore数据库中的文字和照片

【问题讨论】:

  • 你在某个地方使用了这个atob ,而你在使用前没有定义它。
  • 我不是我什至不知道 atob 到底是什么
  • 但错误不在您共享的 sn-ps 中。

标签: javascript node.js reactjs firebase react-native


【解决方案1】:

这是firebase 某些版本中的错误。

一种解决方法是在app.js 中导入base64 并定义它以防未定义。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-07
    • 1970-01-01
    • 2018-02-26
    • 2018-05-28
    • 2021-08-04
    • 2018-10-26
    • 1970-01-01
    • 2021-10-10
    相关资源
    最近更新 更多