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