[英]Why am I getting TypeErrors 'x' is not a function with firebase collection query?
My js file is correctly entered in the DOM, I have initailized firebase in the firebase.js file.我的 js 文件在 DOM 中正确输入,我在 firebase.js 文件中初始化了 firebase。 Why am I getting a
为什么我得到一个
TypeError 'collection(...).doc is not a function' -
TypeError 'collection(...).doc 不是函数' -
The collection query is taken directly from the firebase docs site, I don't understand how this could be a type error.. Any ideas?集合查询直接取自 firebase 文档站点,我不明白这怎么可能是类型错误。有什么想法吗?
import { app } from "./firebase";
import { getFirestore, doc, collection } from "firebase/firestore";
const db = getFirestore(app);
// get data
const docRef = collection(db, 'posts').doc('ARt1ctrEjweKEd4gmgCr');
await docRef.get();
if (!doc.exists) {
console.log('No such document!');
} else {
console.log('Document data:', doc.data());
}
The doc()
is top level function in Modular SDK and not a method on collection()
. doc()
是模块化 SDK 中的顶级 function ,而不是collection()
上的方法。 Try:尝试:
import { getFirestore, doc, getDoc } from "firebase/firestore";
// Creating DocumentReference
const docRef = doc(db, 'posts', 'ARt1ctrEjweKEd4gmgCr');
const docSnap = await getDoc(docRef);
if (!doc.exists) {
console.log('No such document!');
} else {
console.log('Document data:', doc.data());
}
Also checkout: Firestore: What's the pattern for adding new data in Web v9?另请查看: Firestore:在 Web v9 中添加新数据的模式是什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.