如何在Vue.js组件中使用Firestore修复Firebase collection()错误?

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

How to fix Firebase collection() error in Vue.js component using Firestore?

问题

无法连接到 Firebase:
我的初始化代码 init.js 中的代码如下:

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

const firebaseConfig = {
  apiKey:.. ,
  authDomain: ,
  projectId:..,
  storageBucket: ..,
  messagingSenderId: ..,
  appId: ..,
  measurementId:,
};

initializeApp(firebaseConfig);
const db = getFirestore();
export { db };

然后在一个组件中(我知道它应该在 Vuex 中,但我只是想检查它):

import { collection, addDoc } from "firebase/firestore";
import db from "../firebase/init";

async addQuestion() {
  const colRef = collection(db, "Questions");
  console.log(colRef);
}

我得到以下错误信息:
"Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore
FirebaseError: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore"

在 Firestore 中,我有一个名为 "Questions" 的集合。我尝试了各种不同的方法,但我真的不明白问题出在哪里?

英文:

Cant connect to firebase:
my code in init.js:

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

const firebaseConfig = {
  apiKey:.. ,
  authDomain: ,
  projectId:..
  storageBucket: ..
  messagingSenderId: ..
  appId: ..
  measurementId:,
};


initializeApp(firebaseConfig);
const db = getFirestore();
export { db };

then in a component (I know it should be in vuex but im just trying to check it):

import { collection, addDoc } from "firebase/firestore";
import db from "../firebase/init";

async addQuestion() {
      const colRef = collection(db, "Questions");
      console.log(colRef);

and i get:
Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore
FirebaseError: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore

in firestore a have a collection namde Questions

I tried all diffrent ways and i dont really understand where is a problem?

I tried all diffrent ways and i dont really understand where is a problem?

答案1

得分: 1

我相信你导入的方式不对,试着改成:

import { db } from "../firebase/init";
英文:

I believe you are importing wrong, try to change it to:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

import { db } from &quot;../firebase/init&quot;;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月2日 01:39:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76384403.html
匿名

发表评论

匿名网友

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

确定