【问题标题】:Trouble with firebase imports when upgrading to version 9升级到版本 9 时出现 firebase 导入问题
【发布时间】:2021-12-29 11:16:07
【问题描述】:

我尝试迁移到 firebase 版本 9,并在不更改代码的情况下更新了所有导入,现在当我尝试进行谷歌登录时,我不断得到这个

TypeError: Cannot read properties of undefined (reading 'GoogleAuthProvider')

这是我遇到问题的代码

import firebase from "firebase/compat/app";
   const provider = new firebase.auth.GoogleAuthProvider();
// Import the functions you need from the SDKs you need
import { initializeApp } from "firebase/app";
firebaseConfig

// Initialize Firebase
const app = initializeApp(firebaseConfig);

export default app

【问题讨论】:

标签: javascript firebase google-cloud-platform google-cloud-firestore


【解决方案1】:

我建议您阅读从 v8 更新到 Firebase v9 的指南,因为我看到您之前确认您只更新了导入,但恐怕您需要这样做即使您提到身份验证存在问题,也可以使用一些额外功能来完全升级您的代码,并且主要步骤之一是从以前的版本中删除兼容代码,尤其是在该模块中。步骤如下:

About the upgrade process

升级过程的每个步骤都有范围,因此您可以完成对应用程序源代码的编辑,然后编译并运行它而不会损坏。总而言之,升级应用的步骤如下:

  1. 将版本 9 库和兼容库添加到您的应用中。
  2. 将代码中的导入语句更新为 v9 兼容。
  3. 将单个产品(例如身份验证)的代码重构为模块化样式。
  4. 可选:此时,移除 Authentication 兼容库和 Authentication 兼容代码,以便在继续之前实现 Authentication 的应用程序大小优势。
  5. 将每个产品(例如 Cloud Firestore、FCM 等)的功能重构为模块化样式,编译和测试直到所有区域都完成。
  6. 将初始化代码更新为模块化样式。
  7. 从您的应用中删除所有剩余的第 9 版兼容语句和兼容代码。

您收到此错误是因为您用于初始化 GoogleAuthProvider 的代码来自 v8,因此请按照 official documentation 的建议将其替换为以下代码:

import  {  GoogleAuthProvider  } from "firebase/auth";  
const provider =  new  GoogleAuthProvider();

如果您按照之前的指南缺少步骤,则错误可能会消失

最后,我看到你的导入下有firebaseConfig,我不确定你是否真的这样称呼它,但我会关注更新后的guide

import  { initializeApp }  from  'firebase/app';   
 
    firebaseConfig =  { 
        //...  
    };  
      
const app = initializeApp(firebaseConfig);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    • 2020-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-09
    • 1970-01-01
    相关资源
    最近更新 更多