Home > Net >  V 9 FirebaseError: Expected first argument to collection() to be a CollectionReference, a DocumentRe
V 9 FirebaseError: Expected first argument to collection() to be a CollectionReference, a DocumentRe

Time:09-24

can't add new document to firestore I am new to vue and firestore. This is my config file. [this is my DataServices directory>>C:\Users\LENOVO\Desktop\covidcare-ytu-clinic\src\Services\DataServices.js][1]This is vue directory>>C:\Users\LENOVO\Desktop\covidcare-ytu-clinic\src\views\ClientForm.vue

firebase.js

import { initializeApp } from "firebase/app"
import { getAuth, connectAuthEmulator } from "firebase/auth"
import { getFirestore, connectFirestoreEmulator } from "firebase/firestore"

const firebaseConfig = {
  apiKey: 
  projectId: "covidcare-ytu-clinic",
  
}

// Initialize Firebase
const firebaseApp = initializeApp(firebaseConfig)

const db = getFirestore()

connectFirestoreEmulator(db, "localhost", 8081)

const auth = getAuth(firebaseApp)
connectAuthEmulator(auth, "http://localhost:9099")

export default { db, auth }

This is my Dataservice.js

import { db } from "@/firebase"
import { addDoc, collection } from "firebase/firestore"
class dataService {
  /* async getAll() {
    const querySnapshot = await getDocs(collection(db, "clients"))
    querySnapshot.forEach((doc) => {
      console.log(doc.id, "=> ", doc.data())
    })
  }*/
  async create(data) {
    try {
      const docRef = await addDoc(collection(db, "cli"), data)
      console.log("Document written with ID: ", docRef.id)
    } catch (e) {
      console.error("Error adding document: ", e)
    }
  }
  /*
  update(id, data) {
    return db.collection("clients").doc(id).updateDoc(data)
  }
  delete(id) {
    return db.collection("clients").doc(id).delete()
  }*/version-8 firebase
}
export default new dataService()

Here is my firebase error ..I couldn't add any document to my firestore.This is stacking me so long .


DataServices.js:15 Error adding document:  FirebaseError: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore

CodePudding user response:

In Dataservice.js, try using relative path for importing db:

// Remove this
// import { db } from "@/firebase"

// Try this
import { db } from "../../firebase" // relative path to config file

CodePudding user response:

I found the answer .When somebody face this kind of problems like me , i hope this helpful . i delete default in config file where i should write export { db ,auth} .

  • Related