简体   繁体   English

无法读取未定义的属性“auth”

[英]Cannot read property 'auth' of undefined

I'm trying to use firebase-ui in a VueJS project.我正在尝试在 VueJS 项目中使用 firebase-ui。

My api credentials is defined in a file called config.js我的 api 凭据在名为config.js的文件中定义

export default {
apiKey: "*****",
authDomain: "*****.firebaseapp.com",
databaseURL: "https://my-project.firebaseio.com",
projectId: "*****",
storageBucket: "*****",
messagingSenderId: "73482979",
appId: "1:685818581200:web:1f5ebjnfsdjnj",
measurementId: "G-BHJK6N67PZ"
};

I am the importing the config.js file in my init.js where the whole firebase setup is done:我正在我的init.js中导入config.js文件,其中完成了整个firebase设置:

import config from "./config";
import firebase from "firebase";
import firebaseui from "firebaseui";
import "firebase/auth";
import "firebase/firestore";

const app = firebase.initializeApp(config);
const auth = firebase.auth();
const firestore = app.firestore();

const authUi = new firebaseui.auth.AuthUI(auth); //Error is thrown at this point

export default app;
export { auth, authUi, firestore };

However the error -> Cannot read property 'auth' of undefined' is thrown and I've been unable to move past here for a few days now.然而,错误 -> Cannot read property 'auth'未定义的Cannot read property 'auth'被抛出,我已经有几天无法从这里过去了。 I've checked the documentation ( https://firebase.google.com/docs/auth/web/firebaseui#before_you_begin ), everything is done correctly and even using the latest firebaseui version "firebaseui": "4.7.0" located in package.json我检查了文档( https://firebase.google.com/docs/auth/web/firebaseui#before_you_begin ),一切都正确完成,甚至使用最新的 firebaseui 版本"firebaseui": "4.7.0"位于包.json

Any help with how I can solve this problem?对我如何解决这个问题有什么帮助吗?

You're importing the Firebase client JS library incorrectly.您错误地导入了 Firebase 客户端 JS 库。 The documentation for module bundlers shows: 模块捆绑器文档显示:

// Firebase App (the core Firebase SDK) is always required and must be listed first
import firebase from "firebase/app";
// If you are using v7 or any earlier version of the JS SDK, you should import firebase using namespace import
// import * as firebase from "firebase/app"

Don't import from "firebase".不要从“firebase”导入。 Import from "firebase/app", and be sure to observe the conventions for the version of the SDK you're using.从“firebase/app”导入,并确保遵守您使用的 SDK 版本的约定。

The documentation for firebaseui might be out of date. firebaseui 的文档可能已经过时。 Consider submitting your feedback using the "send feedback" button at the top of the doc page.考虑使用文档页面顶部的“发送反馈”按钮提交您的反馈。

I got the same problem.我遇到了同样的问题。 After few hours of trying, I solve it by edit my config like :经过几个小时的尝试,我通过编辑我的配置来解决它:

import firebase from 'firebase/app';
import * as firebaseui from 'firebaseui';
import 'firebaseui/dist/firebaseui.css';

If your firebase version if greater than 7, you got to import from 'firebase/app' instead of 'firebase'.如果您的 firebase 版本大于 7,则必须从“firebase/app”而不是“firebase”导入。

And you have to import * from 'firebaseui', instead of import only firebaseui.而且你必须从'firebaseui'导入* ,而不是只导入firebaseui。

and my packages.json like:和我的packages.json 像:

"dependencies": {
  "firebase": "^8.8.0-202162022140",
  "firebaseui": "^4.8.1",
},

By the way, my project is using Vue3.js.顺便说一下,我的项目使用的是 Vue3.js。

As of Firebase 9.0.0 (August 25, 2021) it should now be从 Firebase 9.0.0(2021 年 8 月 25 日)开始,它现在应该是

import firebase from 'firebase/compat/app';
import 'firebase/compat/auth';
import 'firebase/compat/firestore';

to use the backwards compatible interface.使用向后兼容的接口。

See https://firebase.google.com/docs/web/modular-upgrade for the full upgrade path有关完整升级路径,请参阅https://firebase.google.com/docs/web/modular-upgrade

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

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