英文:
"'firebase' is not defined" error when using firebase.firestore.timestamp
问题
I'm using Firebase, authentication, and storage without problems in my React app. However, when I tried to use the Firebase timestamp, I'm getting a "'firebase' is not defined" error.
How I use timestamp:
const duedate = moment().add(1, 'd').toDate();
const timestamp = firebase.firestore.Timestamp.fromDate(duedate);
I have Firebase initialized in a different file. I tried all possible imports:
import firebase from 'firebase/compat/app';
import * as firebase from 'firebase/compat/app';
import firebase from 'admin/compat/app';
and many variations. Nothing seems to help.
I'm using the latest version of Firebase. Do you have any idea on how to solve it, please?
Edit: This is how I import Firebase in the firebase.js file:
import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";
import { getAuth } from "firebase/auth";
import { getStorage } from "firebase/storage";
const firebaseConfig = {
};
export const app = initializeApp(firebaseConfig);
export const db = getFirestore(app);
export const auth = getAuth(app);
export const storage = getStorage(app);
(Note: The code has been translated, and only the code portions have been provided as per your request.)
英文:
I'm using firebase, authentication and storage without problems in my React app. However, when I tried to use the firebase timestamp, I'm getting "'firebase' is not defined" error.
How I use timestamp:
const duedate = moment().add(1, 'd').toDate();
const timestamp = firebase.firestore.Timestamp.fromDate(duedate)
I have firebase etc. initialized in a different file. I tried all possible imports:
import firebase from 'firebase/compat/app;
import * as firebase from 'firebase/compat/app;
import firebase from 'admin/compat/app;
and many variations. Nothing seems to help.
I'm using the latest version of firebase. Do you have any idea on how to solve it please?
Edit: This is how I import Firebase in the firebase.js file:
import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";
import { getAuth } from "firebase/auth";
import { getStorage } from "firebase/storage";
const firebaseConfig = {
};
export const app = initializeApp(firebaseConfig);
export const db = getFirestore(app);
export const auth = getAuth(app);
export const storage = getStorage(app);
答案1
得分: 0
我在多次尝试和错误后找到了一个答案,也许对其他迷茫的初学者有帮助。
问题在于我需要导入下面的所有内容,而不仅仅是firebase,除了我已经从我的配置文件中导入的配置。
当只导入下面的第一行时,它仍然显示错误'firebase未定义'。
import firebase from 'firebase/compat/app';
import 'firebase/compat/firestore';
英文:
I found an answer after many trials and errors, maybe it helps another lost beginner.
The problem was that I needed to import all the below, and not just firebase, apart from the configuration that I've already been importing from my config file.
When importing only the first line below, it was still showing me the error 'firebase is not defined'.
import firebase from 'firebase/compat/app';
import 'firebase/compat/firestore';
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论