我已经创建了一个名为sample的集合,其中包含一个随机生成的文档id的4-5个文档。每个文档都包含我要检索的特定字段"msg_id“。我不想使用文档id检索它。有什么办法吗?
我尝试使用以下脚本:
    const db = getFirestore();
    
    const queryref = await db.collection('sample').where("msg_id", "==", 1234).get();但我看不出数据
请查阅下表:

发布于 2022-11-14 22:41:13
乍一看,您的逻辑似乎是正确的,但我认为您面临的问题是,您混合了两个不同的版本,从而导致了一个错误。getFirestore()是Web 9(模块化版本),db.collection('sample').where("msg_id", "==", 1234).get();是Web 8(命名空间版本)的语法。
我假设您使用的是Web 9,因为您具有getFirestore()初始化。请参阅下面的示例代码,以便使用具有特定字段msg_id的where查询获取文档
import { initializeApp } from "firebase/app";
import { collection, query, where, getDocs, getFirestore } from "firebase/firestore";
const firebaseConfig = {
  // Your firebase config.
};
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
const q = query(collection(db, "sample"), where("msg_id", "==", 1234));
const querySnapshot = await getDocs(q);
querySnapshot.forEach((doc) => {
  // doc.data() is never undefined for query doc snapshots
  console.log(doc.id, " => ", doc.data());
});如果您使用的是web 8版本,请告诉我。
有关更多信息,您可以签出此文档。
https://stackoverflow.com/questions/74381191
复制相似问题