1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Firebaseを触ってみたらバージョンの違いで初っ端躓いた

Last updated at Posted at 2022-09-20

問題

Firebaseを認証してfirestoreを使用しようとしたらエラーが出た。

間違っているスクリプト

import firebase from "firebase/app";
import "firebase/app";
import "firebase/firestore";
import "firebase/auth";

const firebaseApp = firebase.initializeApp({
    ...
  });

export const db = firebaseApp.firestore();
export const auth = firebase.auth();

下記のエラーが出た

Property 'firestore' does not exist on type 'FirebaseApp'

Property 'auth' does not exist on type 'typeof import("/Users/.../node_modules/firebase/app/dist/app/index")'

原因・解決策

compatを足すだけ
参考にしていたスクリプトがv8のものだったのが原因である。
インストールしたものはv9だった。

import firebase from "firebase/compat/app";
import "firebase/compat/app";
import "firebase/compat/firestore";
import "firebase/compat/auth";


const firebaseApp = firebase.initializeApp({
    ...
  });

export const db = firebaseApp.firestore();
export const auth = firebase.auth();

compatライブラリについて詳細は公式ページ

そもそもv9の書き方に倣えば。

import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";
import { getFirestore } from "firebase/firestore";

// Initialize Firebase
const app = initializeApp({
    //...
    });


const auth = getAuth(app);
export const db = getFirestore(app);
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?