【问题标题】:Can't find a variable atob找不到变量 atob
【发布时间】:2020-06-07 05:35:55
【问题描述】:

Screen1.js

import React,{useEffect} from 'react'
import {View,Text} from 'react-native'
import * as firebase from 'firebase/app';
import '@firebase/firestore';

const Screen1 = props =>{

    useEffect(() => 
    {
        var dbh = firebase.firestore().collection("Jots").doc("note");
        dbh.set({name:"pradeep"}) //The yellow warning is popped up in this line.
    }); 

    return(
             <View>
                <Text>Title</Text>
             </View>
    )
}

控制台

[Unhandled promise rejection: ReferenceError: Can't find variable: atob]

Stack trace:
  node_modules\@firebase\firestore\dist\index.cjs.js:23101:0 in <global>
  http://192.168.0.108:19001/node_modules%5Cexpo%5CAppEntry.bundle?platform=android&dev=true&minify=false&hot=false:131203:60 in fromBase64String


我只是按照博览会文档中的指南进行操作,但仍然不知道为什么会出现此问题。对此需要一个明确的解释。还有什么是 atob 变量?

【问题讨论】:

  • 除非某些包碰巧命名了类似的名称,否则atob 是一个解码 Base64 编码字符串的函数。另一方面,btoa 将字符串编码为 Base64 (Base64 encoding and decoding)
  • 感谢您的回复。为什么这个 atob 警告会显示在控制台中?你能找出代码中的问题吗?
  • 可能是因为 atob 函数是浏览器环境中window 的一部分,除非特别移植,否则它不可用。你可以尝试下载一个 NPM 模块。
  • 对于 React Native 使用 react-native-firebase.
  • 有人可以解释如何连接到 Firestore 以及为什么会发生 atob 吗?

标签: javascript firebase react-native expo


【解决方案1】:

我已尝试降级,但这并不是我的解决方案。我不知道为什么。

app.jsbase64的全局导入解决了这个问题。

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

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

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

感谢您的回复。

【讨论】:

  • 我升级了 Firebase 7.9.3,但什么也没做。这个解决方案为我解决了这个问题。
  • 也和7.11.0一样,回答解决问题
  • 这里与7.14.0相同
  • 非常感谢。这个解决方案对我有用,同时面临与 7.14.0 相同的问题!
  • 对于 TypeScript,我必须添加 const globalAny: any = global; 才能使其工作,然后使用 globalAny 字段而不是 global
【解决方案2】:

为我工作!谢谢papay0,7.9.1版本确实有问题。

npm install firebase@7.9.0
expo r -c # restard expo without cache

你可以rm -rf ./node_modules/ &amp;&amp; npm i

【讨论】:

    【解决方案3】:

    我找到了一种解决方法,但我仍然有他们这边的错误。 他们刚刚在 2 天前发布了 7.9.1 版。 尝试使用 7.9.0。

    纱线添加 firebase@7.9.0

    我正在为它创建一个问题,关注here

    【讨论】:

    • 感谢您的回复,但降级到较低版本并没有帮助。它仍然说“找不到变量 atob”。
    • 我应该用 atob 包装 JSON 吗?
    【解决方案4】:

    感谢@Pradeep。这在 firebase 7.14.1 上对我有用:

    import {decode, encode} from 'base-64'
    
    if (!global.btoa) {  global.btoa = encode }
    
    if (!global.atob) { global.atob = decode }
    

    并像这样使用导入

    import firebase from 'firebase';
    import 'firebase/firestore'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-22
      • 2016-04-30
      • 2020-07-06
      • 2021-03-25
      相关资源
      最近更新 更多