【问题标题】:V 9 FirebaseError: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestoreV 9 FirebaseError:collection() 的第一个参数应该是 CollectionReference、DocumentReference 或 FirebaseFirestore
【发布时间】:2021-11-16 19:50:15
【问题描述】:

无法向 Firestore 添加新文档 我是 vue 和 firestore 的新手。 这是我的配置文件。 [

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 }

这是我的 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()

这是我的 firebase 错误 ..我无法将任何文档添加到我的 firestore。这让我太长了。


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

【问题讨论】:

  • 您能在addDoc() 声明之前console.log(db) 并分享输出截图吗?我会尝试使用相对路径而不是 @/firebase 导入一次。
  • 是的,先生@Dharmaraj 我在 addDoc() 之前已经 console.log(db) 了。
  • 你能分享一下它记录的截图吗?
  • 在控制台中。 >> 未定义的出现。
  • 那是路径错误..你能分享一下你的目录结构的截图吗?

标签: javascript firebase google-cloud-firestore


【解决方案1】:

Dataservice.js,尝试使用相对路径导入db:

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

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

【讨论】:

  • 仍然遇到同样的错误,先生
  • @Bubblegum 你能分享你的目录结构吗?
  • 是的@Dharmaraj 我重新编辑我的问题先生
  • @Bubblegum 在数据服务中,您是否将导入路径更改为import { db } from "../firebase"?或../../firebase 在我的回答中
  • 从 Dataservice.js 中的“../firebase”导入 { db} 先生
【解决方案2】:

我找到了答案。当有人遇到像我这样的问题时,我希望这会有所帮助。 我在配置文件中删除 default 我应该写 export { db ,auth} .

【讨论】:

    猜你喜欢
    • 2021-11-02
    • 2021-11-01
    • 1970-01-01
    • 2022-07-02
    • 2021-11-01
    • 2023-03-15
    • 2021-11-08
    • 1970-01-01
    • 2021-12-21
    相关资源
    最近更新 更多