[英]Firebase error while using firestore in NextJS
I'm using firebase for my project, and I was able to successfully implement authentication in my app.我正在为我的项目使用 firebase,并且我能够在我的应用程序中成功实现身份验证。 Unfortanetly, when I try to use firestore in my project I have an error that tells me to initialize my app, but I already have a fie for this, and I use every firebase method from there.不幸的是,当我尝试在我的项目中使用 firestore 时,我有一个错误告诉我初始化我的应用程序,但我已经有了一个 fie,我使用了那里的每个 firebase 方法。
import firebase from "firebase/app";
import "firebase/auth"; // If you need it
import "firebase/firestore"; // If you need it
import "firebase/storage"; // If you need it
const clientCredentials = {
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
databaseURL: process.env.NEXT_PUBLIC_FIREBASE_DATABASE_URL,
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID,
appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID,
};
if (typeof window !== "undefined" && !firebase.apps.length) {
firebase.initializeApp(clientCredentials);
if ("measurementId" in clientCredentials) firebase.analytics();
}
export default firebase;
Every time I want to use firebase, I just import firebase from this file and use it, for example每次我想使用firebase时,我只是从这个文件中导入firebase并使用它,例如
firebase.firestore()
Am I doing something wrong?难道我做错了什么?
I don't know if this will help you but this is exactly how I do it.我不知道这是否对你有帮助,但这正是我做的。
// init.js
import firebase from 'firebase/app';
import 'firebase/auth';
import 'firebase/storage';
import 'firebase/firestore';
const firebaseConfig = {
// configs
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
// Enable Firestore Cache
firebase.firestore()
.enablePersistence()
.catch((err) => {
console.error(err);
});
export default firebase;
// firestore.js
import firebase from './init';
/* FIRESTORE
*********************/
export const firestore = firebase.firestore();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.