简体   繁体   English

为什么我收到 TypeErrors 'x' is not a function with firebase 集合查询?

[英]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.

相关问题 firebase.database 不是 function 为什么我在反应本机应用程序中收到此错误? - firebase.database is not a function why I am getting this error in react native app? 为什么在将 env 变量加载到 Firebase function 时我得到未定义? - Why am I getting undefined when loading env variables into Firebase function? 为什么我在 iOS(颤振应用程序)中收到 Firebase AppCheck 错误 - Why am I getting a Firebase AppCheck error in iOS (Flutter app) 为什么我在使用 firebase 时收到尝试导入错误? - Why am I getting the attempted import error when using firebase? 为什么我没有从 firestore 获得查询结果? - Why am I not getting query results from firestore? 为什么在 Google BigQuery 上安排查询时出现错误? - Why am I getting an error when scheduling a query on Google BigQuery? 为什么我没有从反应本机应用程序中的 firebase 实时数据库获取 uuid id - Why I am not getting uuid id from firebase realtime database in react native app 为什么 db.collection 不适用于 firebase。我正在使用来自 next-auth 的 next.js w/ useSession() - Why is db.collection not working for firebase. I am using next.js w/ useSession() from next-auth 为什么 firebase 无法在回收站视图中查询集合中的数据? - Why firebase is not able to query data from collection in recycler view? Firebase 集合上的多个查询 - Multiple Query on Firebase Collection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM