“‘firebase’ is not defined” 当使用 firebase.firestore.timestamp 时发生错误。

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

"'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';

huangapple
  • 本文由 发表于 2023年6月1日 03:08:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76376574.html
匿名

发表评论

匿名网友

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

确定