【问题标题】:Vue Firebase firebase/auth not working but firebase/firestore works fine. "auth is not a function"Vue Firebase firebase/auth 不工作,但 firebase/firestore 工作正常。 “身份验证不是函数”
【发布时间】:2021-05-08 03:04:31
【问题描述】:

知道为什么我的 { auth } 函数“不是函数”但我的 { db } 函数工作得很好吗?示例文件如下。

我在尝试调用 login() 方法时遇到的错误是

[Vue warn]: Error in created hook: "TypeError: _firebase__WEBPACK_IMPORTED_MODULE_2__.auth.auth is not a function"

使用 Vue 2 并通过 npm install firebase --save 安装 firebase。

src/firebase.js

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

const firebaseConfig = {
  apiKey: 'xx',
  authDomain: 'xx',
  databaseURL: 'xx',
  projectId: 'xx',
  storageBucket: 'xx',
  messagingSenderId: 'xx',
  appId: 'xx',
  measurementId: 'xx',
};
firebase.initializeApp(firebaseConfig);
const db = firebase.firestore();
const auth = firebase.auth();

export { db, auth };

login.vue

import { auth } from '@/firebase';
login() {
  try {
      auth
        .auth()
        .signInWithEmailAndPassword(this.email, this.password)
        .then(() => {
          console.log(response);
        })
        .catch((error) => {
          console.log(error);
        });
  } catch (error) {
    console.log(error);
  }
},

文档中的示例代码,运行良好

import { db } from '@/firebase';
var docRef = db.collection("cities").doc("SF");

docRef.get().then((doc) => {
    if (doc.exists) {
        console.log("Document data:", doc.data());
    } else {
        // doc.data() will be undefined in this case
        console.log("No such document!");
    }
}).catch((error) => {
    console.log("Error getting document:", error);
});

【问题讨论】:

    标签: javascript firebase vue.js authentication


    【解决方案1】:

    既然你这样做了

    const auth = firebase.auth();
    export { db, auth };
    

    你不应该这样做

    login() {
      try {
          auth
            .auth()
            .signInWithEmailAndPassword(this.email, this.password)
            ...
    

    但是

    login() {
      try {
          auth
            .signInWithEmailAndPassword(this.email, this.password)
            ...
    

    Firestore 运行良好,因为您正确地执行了 db.collection("cities").doc("SF"); 而不是 db.firestore().collection("cities").doc("SF");

    【讨论】:

    • 天哪,我花了大约两个小时在这上面,并没有注意到这一点。我会在今天晚些时候回复它,完成后我会标记它已解决:) 谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-18
    • 2018-02-13
    • 2018-11-30
    • 2016-12-04
    • 1970-01-01
    相关资源
    最近更新 更多