未找到 Firebase 导入。

huangapple go评论64阅读模式
英文:

Not find firebase import

问题

这是我的firebase.js

import { initializeApp } from 'firebase/app';
import { getAuth } from 'firebase/auth';
import { getStorage } from 'firebase/storage';
import { getFirestore } from 'firebase/firestore';

const firebaseConfig = {
    apiKey: 'AIzaSyCUndICAUxUT3n_Q19F05BazSpLOkr2d1c',
    authDomain: 'chat-e7246.firebaseapp.com',
    projectId: 'chat-e7246',
    storageBucket: 'chat-e7246.appspot.com',
    messagingSenderId: '28357679434',
    appId: '1:28357679434:web:58376c850f47e39fb689b3',
    measurementId: 'G-VE6BZN6595',
};

// Initialize Firebase
export const app = initializeApp(firebaseConfig);
export const auth = getAuth();
export const storage = getStorage();
export const db = getFirestore();

当我尝试在user.jsx文件中使用它来创建数据库时,出现如下错误:Error registering user: TypeError: firebase__WEBPACK_IMPORTED_MODULE_6_.auth.createUserWithEmailAndPassword 不是一个函数。

英文:

This is my firebase.js

import { initializeApp } from 'firebase/app';
import { getAuth } from 'firebase/auth';
import { getStorage } from 'firebase/storage';
import { getFirestore } from 'firebase/firestore';

const firebaseConfig = {
    apiKey: 'AIzaSyCUndICAUxUT3n_Q19F05BazSpLOkr2d1c',
    authDomain: 'chat-e7246.firebaseapp.com',
    projectId: 'chat-e7246',
    storageBucket: 'chat-e7246.appspot.com',
    messagingSenderId: '28357679434',
    appId: '1:28357679434:web:58376c850f47e39fb689b3',
    measurementId: 'G-VE6BZN6595',
};

// Initialize Firebase
export const app = initializeApp(firebaseConfig);
export const auth = getAuth();
export const storage = getStorage();
export const db = getFirestore();

When i try to use it to create DB in user.jsx file

import { auth, db } from '~/firebase';
const onSubmit = (data) => {
        setFormData({
            email: data.email,
            fullname: data.fullname,
            username: data.username,
            password: data.password,
            confirmPassword: data.confirmPassword,
        });
    };

    const registerUser = async () => {
        try {
            const { user } = await auth.createUserWithEmailAndPassword(formData.email, formData.password);
            await db.collection('users').doc(user.uid).set(formData);

            return user;
        } catch (error) {
            console.error('Error registering user:', error);
            throw error;
        }
    };

The error comes like this : Error registering user: TypeError: firebase__WEBPACK_IMPORTED_MODULE_6_.auth.createUserWithEmailAndPassword is not a function

答案1

得分: 1

你的配置文件 firebase.js 不正确。你应该将 app 变量作为参数传递给 getAuthgetStoragegetFirestore 函数...
像这样:

// 初始化 Firebase
export const app = initializeApp(firebaseConfig);
export const auth = getAuth(app);
export const storage = getStorage(app);
export const db = getFirestore(app);
英文:

Your configuration file firebase.js isn't right. You should pass app variable as argument to getAuth, getStorage, and getFirestore function...
Like this:

// Initialize Firebase
export const app = initializeApp(firebaseConfig);
export const auth = getAuth(app);
export const storage = getStorage(app);
export const db = getFirestore(app);

答案2

得分: 1

是的,我遵循了指南,传递了app变量但仍然出现错误。然后我通过以下方式修复它:

 const onSubmit = (data) => {
        setFormData({
            email: data.email,
            fullname: data.fullname,
            username: data.username,
            password: data.password,
            confirmPassword: data.confirmPassword,
        });
    };

    const registerUser = async () => {
        try {
            const { user } = await createUserWithEmailAndPassword(auth, formData.email, formData.password);
            await setDoc(doc(db, 'users', user.uid), {
                username: formData.username,
                password: formData.password,
                email: formData.email,
            });

            return user;
        } catch (error) {
            console.error('Error registering user:', error);
            throw error;
        }
    };

这是我的firebase.js

export const app = initializeApp(firebaseConfig);
export const auth = getAuth(app);
export const storage = getStorage(app);
export const db = getFirestore(app);

英文:

Yeah, after I follow the guide and pass app variable but still error. Then I fix it by doing this :

 const onSubmit = (data) => {
        setFormData({
            email: data.email,
            fullname: data.fullname,
            username: data.username,
            password: data.password,
            confirmPassword: data.confirmPassword,
        });
    };

    const registerUser = async () => {
        try {
            const { user } = await createUserWithEmailAndPassword(auth, formData.email, formData.password);
            await setDoc(doc(db, 'users', user.uid), {
                username: formData.username,
                password: formData.password,
                email: formData.email,
            });

            return user;
        } catch (error) {
            console.error('Error registering user:', error);
            throw error;
        }
    };

And this is my firebase.js

export const app = initializeApp(firebaseConfig);
export const auth = getAuth(app);
export const storage = getStorage(app);
export const db = getFirestore(app);

huangapple
  • 本文由 发表于 2023年3月9日 23:07:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686455.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定