Cloud Firestore:如何在我的集合查询中获取文档引用并将其映射为JSON值? [英] Cloud Firestore: how to fetch a document reference inside my collection query and map it as a JSON value?

查看:78
本文介绍了Cloud Firestore:如何在我的集合查询中获取文档引用并将其映射为JSON值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有很多评论。每个评论对象对发布用户都有一个文档引用。我需要一个查询,该查询将返回包含每个单个用户引用值的注释列表,因此我的查询返回格式良好的Json注释对象。

Let's say I have a collection of comments. Every comment object has a "doc ref" to the user who posted. I need a query that will return a list of comments including the value of every single user reference, so my query returns a nice formatted of Json comment objects.

推荐答案

此处提出了类似的问题什么是firestore参考数据类型好吗?,我认为无法根据此答案 https来完成您要问的事情://stackoverflow.com/a/46570119/473453

A similar question was asked here What is firestore Reference data type good for?, I don't think it is possible to do what you are asking according to this answer https://stackoverflow.com/a/46570119/473453.

您必须自己加载每个引用,例如

You have to load every reference yourself, e.g.

const comments = []
firebase.firestore().collection('/comments').get().then(snapshot => {
  snapshot.docs.forEach(doc => {
    const comment = doc.data()
    comment.userRef.get().then(snap => {
      comment.user = snap.data()
      comments.push(comment)
    })
  })
})

对于很多评论,这会增加很多开销。也许您可以编写一个CloudFunction来在服务器端为您完成所有工作并返回格式化的JSON。

For many comments this will add a lot of overhead. Maybe you can write a CloudFunction that does the work for you all on the server side and returns you a formatted JSON.

看起来他们可能正在努力在未来虽然如此: https://stackoverflow.com/a/46614683/473453

It looks like they might e working on supporting this in the future though: https://stackoverflow.com/a/46614683/473453

这篇关于Cloud Firestore:如何在我的集合查询中获取文档引用并将其映射为JSON值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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