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

查看:68
本文介绍了在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?

理想的结果如下:

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

推荐答案

当您需要访问其他元数据(例如文档的键)时,可以使用snapshotChanges()流方法.

When you need to access additional metadata like the key of your Document, you can use the snapshotChanges() streaming method.

firestore.collection("categories").valueChanges().map(document => {
      return document(a => {
        const data = a.payload.doc.data();//Here is your content
        const id = a.payload.doc.id;//Here is the key of your document
        return { id, ...data };
      });

您可以查看文档以获得进一步的解释和例子

You can review the documentation for further explanation and example

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

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