【问题标题】:undefined is not a function (evaluating '_iterator2[typeof Symbol === "function"undefined 不是函数(评估 '_iterator2[typeof Symbol === "function"
【发布时间】:2018-08-29 10:55:42
【问题描述】:

我正在使用 react native 实现 firebase,这是我正在使用的版本

"firebase": "^5.4.1",
"react-native": "0.56.0",
"react": "16.4.1",

但是当我导入 firebase 时,它​​显示以下错误:

undefined is not a function (evaluating '_iterator2[typeof Symbol === "function" ? Symbol.iterator : "@@iterator"]()')

注意:仅在 Android 设备上出现此错误,在 iOS 上运行完美。

【问题讨论】:

  • 你添加了firebase Android原生SDK吗?
  • 使用js版firebase时不需要native实现,使用react-native-firebase库时需要实现
  • 但是你的上下文是 react-native,你不能只使用 JS 的。例如,react-native fetch 具有相同的 API,但在底层实现完全不同。 JS不能用,必须用react-native-firebase
  • 我可以,即使它与 react-native:0.55.4 完美配合,我也有使用 firebase + react native 的实时项目。这只是版本 0.56.0 的问题
  • 同样的问题,但我不使用 firebase。有什么解决办法吗?

标签: firebase react-native react-native-firebase


【解决方案1】:

我在导入firebase时遇到了同样的问题,找到了解决方案here

在你的 index.js 中添加这个

// index.js
global.Symbol = require('core-js/es6/symbol');
require('core-js/fn/symbol/iterator');
require('core-js/fn/map');
require('core-js/fn/set');
require('core-js/fn/array/find');

我的看法是,有些 JS 语法不能很好地翻译为 android 理解。

【讨论】:

  • 这是否意味着它的问题来自firebase 而不是来自react-native 团队?
  • @RaviRupareliya 我是初学者,所以我不确定。但是 RN 桥向 android 发送 json 消息,我假设 firebase 为其迭代器使用新语法,其中 RN 发送一条 android 还不理解的消息。
  • @RaviRupareliya 实际上我建议安装早期版本的 firebase,因为您会发现当前版本存在更多问题。
  • 头部中弹。 Excelente 它对我来说就像一个魅力
【解决方案2】:

我尝试了最重要的解决方案,但解决我的错误的方法是遵循以下步骤:

  1. 创建一个 polyfills 文件代码并放入一个名为 polyfills.js 的文件,与 index.js 处于同一级别(我的 react-原生项目)

  2. 代码:

global.Symbol = require('core-js/es6/symbol');
require('core-js/fn/symbol/iterator');
require('core-js/fn/map');
require('core-js/fn/set');
require('core-js/fn/array/find');
  1. index.js 文件的第一行包含以下代码:
import './polyfills.js';

现在重新加载应用程序(按 R 两次)。希望它有效!

Answer Reference

【讨论】:

    【解决方案3】:

    @Syph 提供的解决方案,不足以让我完美运行,我必须在“index.js”中添加以下内容,它对我有用。

    global.Symbol = require('core-js/es6/symbol');
    require('core-js/fn/symbol/iterator');
    require('core-js/fn/map');
    require('core-js/fn/set');
    require('core-js/fn/array/find');
    
    if (Platform.OS === 'android') {
        if (typeof Symbol === 'undefined') {
            if (Array.prototype['@@iterator'] === undefined) {
                Array.prototype['@@iterator'] = function () {
                    let i = 0;
                    return {
                        next: () => ({
                            done: i >= this.length,
                            value: this[i++],
                        }),
                    };
                 };
            }
        }
    }
    

    【讨论】:

      【解决方案4】:

      可能Android无法理解导入firebase时的这些JS语法。将这些代码添加到您的 index.js 可能会解决它。

      注意:如果您有单独的 index.android.js,请将这些代码添加到该文件中。

      global.Symbol = require('core-js/es6/symbol');
      require('core-js/fn/symbol/iterator');
      require('core-js/fn/map');
      require('core-js/fn/set');
      require('core-js/fn/array/find');
      

      【讨论】:

        猜你喜欢
        • 2018-01-22
        • 2018-10-30
        • 2019-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-05
        • 1970-01-01
        相关资源
        最近更新 更多