简体   繁体   English

我可以获取所有文档,但无法在 Firestore 中检索单个文档

[英]I can get all documents but unable to retrieve single document in Firestore

EDIT:编辑:

Rookie error, I was trying to reference a document by ID but the input I was using had space at the end due to human error.新手错误,我试图通过 ID 引用文档,但由于人为错误,我使用的输入最后有空间。

const id = "kahgsdjhagsd " // should be "kahgsdjhagsd"
const usersRef = fb.db.collection('users').doc(id);

======== ========

I am trying to retrieve a single document yet it indicates it does not exist, despite the fact when I run the logic to get ALL documents it is in the response我正在尝试检索单个文档,但它表明它不存在,尽管事实上当我运行逻辑以获取所有文档时它都在响应中

    const users = await db.collection('users').get();
    users.forEach(user => {
        console.log(user.id, user.data()) // <-- Works! displays ID...
        const userRef = db.collection('users').doc(user.id)
        if (userRef.exists) {
            console.log("User exists")
        } else {
            console.log("User does not exist") // <-- Getting this though
        }
    })

Its very weird, the below works if I hardcode the ID as a string:非常奇怪,如果我将 ID 硬编码为字符串,则以下内容有效:

async getUser() {
    const id = "some-id-to-a-document"
    const usersRef = fb.db.collection('users').doc(id);
    const doc = await usersRef.get();
    if (!doc.exists) {
        console.log('No such document!'); 
    } else {
        console.log('Document data:', doc.data()); // <-- Getting this...
    }
}

But if I try to input an ID via the function...但如果我尝试通过 function 输入 ID...

async getUser(id) {
    console.log(id) // <-- shows the ID!
    const usersRef = fb.db.collection('users').doc(id);
    const doc = await usersRef.get();
    if (!doc.exists) {
        console.log('No such document!'); // <-- Getting this...
    } else {
        console.log('Document data:', doc.data());
    }
}

const userRef = fb.usersCollection.doc(user.id)常量 userRef = fb.usersCollection.doc(user.id)

In the above line, You have to get the DocumentSnapshot using get() before checking the document existence.在上面的行中,您必须在检查文档存在之前使用get()获取DocumentSnapshot

const userSnap = await fb.usersCollection.doc(user.id).get();
if (userSnap.exists) {
    console.log("User exists")
} else {
    console.log("User does not exist")
}

And another important note, within forEach using async / await is not preferable as it won't give expected result always.另一个重要的注意事项是,在forEach中使用 async / await 是不可取的,因为它不会总是给出预期的结果。 The traditional for..loop would be good to get the work done.传统的for..loop可以很好地完成工作。

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

相关问题 Firestore:检索单个文档 - Firestore : Retrieve a single document 如何在Firestore中获取1个文档? - How do I get 1 single document in firestore? 如何在 Firebase Firestore 的每个文档的子集合中获取文档? - How can I get documents inside a subcollection in every document in Firebase Firestore? 如何从 firebase firestore 异步获取所有父 collections 的所有文档? - How can I get all the documents of a all parent collections asynchronously from firebase firestore? Firestore 查询,其中结果有一个字段,该字段包含要传递给第二个查询以检索所有文档的文档引用 - Firestore query where the results have a field that contains a document ref to be passed to a second query to retrieve all the documents 如何检索集合中的所有文档并查看 firebase Firestore 中是否存在文档? - How do to retrieve all documents in a collection and see if a document exists in firebase Firestore? 我可以使用 Promise.all 同时从 Firestore 获取一些文件吗? - can I get some documents from firestore simultaneously using Promise.all? Firestore 从单个文档纯 JS 中获取所有字段 - Firestore get all fields from a single document pure JS 我如何通过反应从 Firestore 中检索一个简单的文档 - How can i retrieve a simple document from firestore through react Firestore 按字段值检索单个文档并更新 - Firestore retrieve single document by field value and update
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM