简体   繁体   English

如何使用 Javascript 读取具有自动生成的文档 ID 的 Firebase 文档

[英]How to read Firebase document with auto generated document id using Javascript

I am trying to read a single document from a Firebase collection using a variable that is populated with the document id.我正在尝试使用填充了文档 ID 的变量从 Firebase 集合中读取单个文档。 The document id is correct and the document exists in the collection.文档 id 正确且文档存在于集合中。 When I initiate the code using variable it seems that the read was successful but there is no data in the field I am interested in. (See Code below).当我使用变量启动代码时,似乎读取成功,但我感兴趣的字段中没有数据。(参见下面的代码)。

 db.collection("Pswref").doc(clpdocid).get().then((docRef) => { const SingleRecord = docRef.data(); console.log('yyyyyy - Great document found using fixed id for doc id ', docRef.data()); console.log('yyyyyy - Title from database = >', SingleRecord.title); console.log('yyyyyy - Doc id ', clpdocid); }).catch((error) => { console.error('yyyyyy - Error getting document ', error); });

However when I hard-code the document id in the get statement all is well and I can access the information needed.但是,当我在 get 语句中对文档 ID 进行硬编码时,一切都很好,我可以访问所需的信息。 (See code below). (见下面的代码)。

 db.collection("Pswref").doc("NoA6EBijcgsBIIOZCsWC").get().then((docRef) => { const SingleRecord = docRef.data(); console.log('xxxxxx - Great document found using fixed id for doc id ',docRef.data()); console.log('xxxxxx - Title from database = >', SingleRecord.title); }).catch((error) => { console.error('xxxxxx - Error getting document ', error); });

The image shows the console.log output as per the script.该图显示了根据脚本的 console.log output。 NB: You will note that the above queries are exactly the same accept for the doc id reference used and I get a different result as per the output generated via the console.注意:您会注意到,上述查询与所使用的 doc id 引用完全相同,并且根据通过控制台生成的 output,我得到了不同的结果。

在此处输入图像描述

Can someone help me to understand why this might happen and what the solution might be?有人可以帮助我了解为什么会发生这种情况以及解决方案可能是什么吗?

I have commented on the line out giving the error and rerun so that the output in the console can show that the clpdocid is actually correctly populated.(see snipped below)and then output on the new image.我已经对给出错误的行进行了注释并重新运行,以便控制台中的 output 可以显示clpdocid实际上已正确填充。(请参见下面的片段)然后在新图像上显示 output。

 db.collection("Pswref").doc(clpdocid).get().then((docRef) => { const SingleRecord = docRef.data(); console.log('yyyyyy - Great document found using fixed id for doc id ', docRef.data()); //console.log('yyyyyy - Title from database = >', SingleRecord.title); console.log('yyyyyy - Doc id ', clpdocid); }).catch((error) => { console.error('yyyyyy - Error getting document ', error); });

在此处输入图像描述

在此处输入图像描述

It's certainly the case that clpdocid doesn't actually contain the value you expect.当然, clpdocid实际上并不包含您期望的值。 The undefined that you're printing from docRer.data() is proof that you've requested a document that doesn't exist, since the API documentation for data() says to expect undefined if the document was not found.您从docRer.data()打印的undefined证明您请求的文档不存在,因为data()的 API 文档说如果找不到文档,则期望未定义。

Try logging the value of clpdocid before the query to make sure you have the document ID you want.尝试在查询之前记录clpdocid的值,以确保您拥有所需的文档 ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase:如何将自动生成的文档 ID 添加到 svelte 中的文档? - Firebase: how to add auto-generated document id to the document in svelte? 如何获取 Firestore 文档的自动生成 ID? - How to get the auto generated ID of a Firestore document? 如何使用 Angular 将自动生成的 Firestore 文档 ID 存储在我的自定义 ID 中 - How to store the auto generated Firestore document ID in my custom ID using Angular 如何在 Firestore Web V9 的子集合中获取新创建的文档的自动生成的 ID? Javascript/反应 - How to get auto-generated ID of newly created document in a subcollection in Firestore Web V9? Javascript/React React Native Firebase 如何让文档 ID 自动递增 1 - React Native Firebase How To Have Auto Document Id Increment by 1 是否可以从 FireStore 对自动生成的 ID 文档进行快照? - Is it possible to snapshot an auto-generated ID document from FireStore? 在 Cloud Firestore 中获取一个特定的自动生成的文档 ID - Take one specific auto-generated ID of a document in Cloud firestore Firebase - 需要使用“where”查找文档 ID - Firebase - Needing to find document id using 'where' 使用文档自动ID Firebase查询“ where”子句 - Querying “where” clause with document auto-id firebase 保存javascript生成的文档 - Save the document generated by javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM