如何解决Firebase:尚未创建Firebase应用程序'[DEFAULT]' - 调用Firebase App.initializeApp()(app/no-app)



我正在尝试使用react和firebase创建一个聊天室应用程序。最初,我在同时导入auth和firestore时出错,即从"react"导入{auth,firestore};,我试过firebase.auth((;和firebase.firestore((;我收到一个错误,说"auth not defined no def"one_answers"firestore not defined no-def",现在我尝试了const auth=firestore.auth((;和const firestore=firebase.firestore((,我现在得到一个错误";Firebase:尚未创建任何Firebase应用程序"[DEFAULT]"-调用Firebase App.initializeApp(((App/No-App("。。。有人能帮我吗

附件是我的代码

import firebase from 'firebase';
//import 'firebase/app';
//import { auth, firestore } from 'firebase';
//firebase.auth();
//firebase.firestore();
const auth = firebase.auth();
const firestore = firebase.firestore()
export const signup = (user) => {
return async (dispatch) => {
const db = firestore();
auth()
.createUserWithEmailAndPassword(user.email, user.password)
.then(user => {
console.log(user); 
})
.catch(error => {
console.log(error);
})
}
}

在使用任何firebase SDK之前,您需要初始化firebase应用程序。

此代码显示如何初始化应用程序:

import firebase from "firebase/app";

// Replace the following with your app's Firebase project configuration
const firebaseConfig = {
apiKey: "AIzaSyDOCAbC123dEf456GhI789jKl01-MnO",
authDomain: "myapp-project-123.firebaseapp.com",
databaseURL: "https://myapp-project-123.firebaseio.com",
projectId: "myapp-project-123",
storageBucket: "myapp-project-123.appspot.com",
messagingSenderId: "65211879809",
appId: "1:65211879909:web:3ae38ef1cdcb2e01fe5f0c",
measurementId: "G-8GSGZQ44ST"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);

不要忘记用firebase项目中的配置替换配置。在这里,您可以看到如何获得Firebase配置。

之后,您可以运行您的代码。

确保在使用任何SDK之前调用初始化。为此,我还建议将您的代码更改为:

import firebase from 'firebase';
//import 'firebase/app';
//import { auth, firestore } from 'firebase';
//firebase.auth();
//firebase.firestore();

export const signup = (user) => {
return async (dispatch) => {
const auth = firebase.auth();
const firestore = firebase.firestore()
const db = firestore();
auth()
.createUserWithEmailAndPassword(user.email, user.password)
.then(user => {
console.log(user); 
})
.catch(error => {
console.log(error);
})
}
}

尝试在index.js 中导入firebase配置文件

import firebase.js

我有同样的问题

const app = initializeApp(firebaseConfig);

如果这些代码都在同一个文件中,请确保这些代码高于您正在导入的任何其他东西,例如provider、auth、getAuth e.tc。只是您正在调用的任何其他方法。。。

发生在我身上也没有任何原因

修复了在根应用中导入带有凭据的配置文件的问题

import app from './firebase/config'
console.log('app :', app);

最新更新