简体   繁体   English

如何使用angularfire从另一个集合中获取firestore中的用户名

[英]how to get a username in firestore from another collection with angularfire

how to get a username in firestore from another collection with angularfire如何使用angularfire从另一个集合中获取firestore中的用户名

I have 2 collections我有 2 个 collections

-users
--username
--uid

and

-post
--title
--user_id

How to get the username when displaying all posts?显示所有帖子时如何获取用户名?

I could only return the user_id我只能返回 user_id

You didn't share your existing code for displaying all posts, so it is difficult to give your the exact code to add...您没有共享用于显示所有帖子的现有代码,因此很难提供要添加的确切代码...

But, basically, you have two options:但是,基本上,您有两种选择:

Lookup the username in the users collection在 users 集合中查找用户名

Before displaying a post , you do an extra query to the database to get the value of the username , like afs.collection('users', ref => ref.where('uid', '==', 'user_id)) (See https://github.com/angular/angularfire/blob/master/docs/firestore/querying-collections.md )在显示post之前,您对数据库进行额外查询以获取username的值,例如afs.collection('users', ref => ref.where('uid', '==', 'user_id)) (见https://github.com/angular/angularfire/blob/master/docs/firestore/querying-collections.md

This implies an extra read for each post you display这意味着您显示的每篇文章都需要额外阅读

Save the username together with the userid when you save the post document保存 post 文档时将用户名与用户 ID 一起保存

This is a classical approach in the NoSQL world: you denormalize your data in such a way your query is easy to build and execute.这是 NoSQL 世界中的经典方法:您以易于构建和执行查询的方式对数据进行非规范化。

More concretely it means that your posts collection would be like:更具体地说,这意味着您的帖子集合将类似于:

-post
--title
--user_id
--username

However, this is only possible if you have, in your front-end, the value of username when you write the post document.但是,只有当您在编写post文档时在前端具有username的值时,这才有可能。 One classical way is to get it from the currentuser object, via currentuser.displayname .一种经典方法是通过currentuser从当前用户currentuser.displayname获取它。

One side effect of this approach is that you need to keep the values in sync (ie the one in the user document and the ones in the post document s ).这种方法的一个副作用是您需要保持值同步(即user文档中的值和post文档中值)。 However, in your specific case, we can probably make the assumption that the username is not going to change very frequently...但是,在您的具体情况下,我们可能会假设用户名不会经常更改......


You should not be afraid to duplicate data and denormalize your data model.您不应该害怕重复数据和非规范化您的数据 model。 Here is a "famous" post about NoSQL data-modelling approaches: https://highlyscalable.wordpress.com/2012/03/01/nosql-data-modeling-techniques/这是关于 NoSQL 数据建模方法的“著名”帖子: https://highlyscalable.wordpress.com/nosql-data

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

相关问题 如何将一个集合作为子集合复制到另一个集合中? Angularfire/ Angular/ Firestore - How do I copy a collection into another, as a sub collection? Angularfire/ Angular/ Firestore AngularFire 根据来自另一个集合的查询获取文档 - AngularFire get documents based on query from another collection 如何从 angularfire firestore 获取多个随机文档 - How to get multiple random documents from angularfire firestore Firestore如何从另一个集合文档id获取集合值 - Firestore how to get the collection value from another collection document id is referenced AngularFire-将来自Firestore集合查询的数据推入数组 - AngularFire - Push data from firestore collection query into an array 将来自firestore的嵌套集合作为angularfire2和firebase的对象返回 - Return nested collection from firestore as object for angularfire2 and firebase 如何在 angularfire firestore 中删除整个文档集合 - How to do remove an entire document collection in angularfire firestore AngularFire-如何在集合查询中显示Firestore键和值 - AngularFire - How to display Firestore key and value in collection query 如何从FireStore获取收藏? - How can I get a collection from FireStore? 如何使用Firebase3中的angularfire2在Google Auth中正确获取个人资料照片和用户名 - How to get profile pic and username properly in google auth using angularfire2 from firebase3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM