在 Firestore 中获取文档名称 [英] Get documents names in Firestore

查看:24
本文介绍了在 Firestore 中获取文档名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从一个集合中获取多个文档时,结果只是一个包含每个文档数据的数组.

When I get several documents from a collection, the result is only an array with each doc data.

firestore.collection("categories").valueChanges().subscribe(data => {
    console.log(data);
    // result will be: [{…}, {…}, {…}]
};

<小时>

如何获取每个文档的名称?


How can I get the name of each doc?

理想的结果应该是这样的:

The ideal result would look like this:

{"docname1": {…}, "docname2": {…}, "docname3": {…}}

推荐答案

// Print each document 
db.collection("categories")
    .onSnapshot((querySnapshot) => {
        querySnapshot.forEach((doc) => {
            console.log(doc.data()); // For data inside doc
            console.log(doc.id); // For doc name
        }
    }

这篇关于在 Firestore 中获取文档名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆