Ankit Sanghvi
Ankit Sanghvi

Reputation: 527

firestore' does not exist on type 'typeof import

Difficulty in setting up this app as firebase, exported from firebase/app doesn't seem to have the firestore() method on it (See attached image). This is my code. Someone please help me fix it.

// Import the functions you need from the SDKs you need
import firebase from 'firebase/app';
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries

// Your web app's Firebase configuration
const firebaseConfig = {
  apiKey: "-------",
  authDomain: "--------",
  projectId: "------------",
  storageBucket: "------------",
  messagingSenderId: "---------",
  appId: "----------"
};

// Initialize Firebase
const app = firebase.initializeApp(firebaseConfig)
export const firestore = firebase.firestore()

firestore() method does not exist on firebase

Upvotes: 2

Views: 1467

Answers (2)

Eugene Maysyuk
Eugene Maysyuk

Reputation: 3378

Here's an example how to do it via firebase-admin lib.

import {onRequest} from "firebase-functions/v2/https"
import {initializeApp} from "firebase-admin/app"
import {getFirestore} from "firebase-admin/firestore"
import express from "express"

initializeApp()
const db = getFirestore()

const app = express()
app.disable("x-powered-by")

app.get("/posts", async (req, res) => {
  await db.collection("expenses").add({title: "Big title"})
  res.send({status: "ok"})
})

const settings =
  {
    region: "europe-central2",
    timeoutSeconds: 5,
    minInstances: 0,
    maxInstances: 5,
  }
export const api = onRequest(settings, app)

Upvotes: 0

ckoala
ckoala

Reputation: 318

Maybe you're using Firestore v9. So you might be using the methods from the old API

You can check what version of firebase you've installed by looking at the major version of the npm package in your package.json

Here is a snippet from the Firestore docs on how to initialize firestore with with the web version v9 (https://firebase.google.com/docs/firestore/quickstart)

// Initialize Cloud Firestore through Firebase
import { initializeApp } from "firebase/app"
import { getFirestore } from "firebase/firestore"
const firebaseApp = initializeApp({
  apiKey: '### FIREBASE API KEY ###',
  authDomain: '### FIREBASE AUTH DOMAIN ###',
  projectId: '### CLOUD FIRESTORE PROJECT ID ###'
});

const db = getFirestore();

Upvotes: 3

Related Questions