英文:
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 "../firebase/init";
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论