0

我需要帮助,我正在观看 Firebase 的教程并做出反应。然后我意识到该教程是针对 v8 的,而最新的是 v9

这是我的 firebase.js

import { initializeApp } from 'firebase/app';
import { getAuth } from 'firebase/auth';
import { getDatabase } from "firebase/database";

const firebaseConfig = {
  //firebaseconfig
};

const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
const database = getDatabase(app);

export { auth, database }

然后这是我的 signin.js 代码段

import { auth, database } from "../Firebase/firebase";

const submitForm = async (values) => {
   
auth()
.signInWithEmailAndPassword(values.email,values.password)
.then((userCredentials) => {
        props.history.push("/profile");
      }).catch((err) => {  
        setLoading(false);
      });
  };

然后当我提交表单时,应用程序在 auth() 上出现错误并崩溃。

Unhandled Rejection (TypeError): Object(...) is not a function
4

1 回答 1

1
import { auth, database } from "../Firebase/firebase";
// this  ^^^^ is not a function but is being used as one
// auth().signInWithEmailAndPassword(values.email,values.password)
// ^^^^ here

signInWithEmailAndPassword需要从firebase/auth如下图导入:

import { auth, database } from "../Firebase/firebase";
import { signInWithEmailAndPassword } from "firebase/auth"

const submitForm = async (values) => { 
  const userCredentials = await signInWithEmailAndPassword(auth, values.email, values.password)
  // Pass the 'auth' instance here                         ^^^^
  const { user: { uid } } = userCredentials
  console.log(`userId: ${uid}`)
}

我建议您遵循文档以及任何教程。该文档具有名称空间 (v8) 和模块化/功能 (v9) 语法。

于 2021-09-12T16:03:19.493 回答