【问题标题】:Problems trying to fetch Cloud Firestore data尝试获取 Cloud Firestore 数据时出现问题
【发布时间】:2020-01-16 19:17:02
【问题描述】:

好的,我的 Cloud Firestore 上有这个结构:

Collection question:
- userID
- text

and Collection user:
- name 
- key

我可以轻松地从数据库中检索问题数据并将其返回,但目前没有用户数据。他们我需要在数据库中再次搜索之前返回的 foreach 评论。但是我在尝试这样做时遇到了很多问题。

第一:

我这样做是为了搜索问题:

组件:

export class ListQuestionsComponent implements OnInit {
  tableData: Question[] = [];
  userData: User[] = [];

constructor(
      private questionService: QuestionService,
      private userService: UserService,
  ) { }

 ngOnInit() {
    this.loadItens();
    this.getUsers();
  }

loadItens() {
    this.questionService.loadItems().subscribe(response => {
      this.tableData = [];
      for (const item of response) {
        this.tableData.push(item.payload.doc.data() as Question);
      }
}

问题服务:

loadItems() {
    const query = ref => ref
    .limit(10);

    return this.firestore.collection('perguntas', query).snapshotChanges();
}

这行得通,现在我的 tableData 中有问题。现在我需要为每个问题搜索用户。

我尝试在同一个组件中执行此操作:

getUsers() {
 for(const item of this.tableData) {
   this.userService.getUserByKey(item.userID).subscribe(response => {
      this.userData = [];
      for (const user of response) {
        this.userData.push(user.payload.doc.data() as User);
      }
   });
 }
}

用户服务

getUserByKey(key: string) {
    return this.firestore.collection('users', ref => ref
      .where('key', '==', key))
      .snapshotChanges();
  }

最后,我有一个包含 10 个问题的 tableData,而 userData 一无所有。我不知道现在该怎么办。我只需要在我寻找的问题中引用的用户。

【问题讨论】:

  • 您是否考虑过存储问题所需的用户数据?这将允许您提出问题并获得所需的所有数据。为此,在提交问题时,您存储用户的数据,如有必要,您可以使用云功能查找未来的用户信息更改(更改时,更新用户存在的所有问题)。

标签: angular typescript firebase google-cloud-firestore


【解决方案1】:

有很多方法,我对 Firestore 采用的方法是将我认为需要的数据与我要提取的数据一起存储。

因此,如果我要提取questions 并且我知道我需要user、他们的username 和他们的profile picture,那么我将在每个具有用户ID 的问题文档中存储一个用户字段、用户名和头像。

类似:

Questions:
---> Question
---> Date
---> User:
-------> ID
-------> Profile Picture
-------> Username

更新:

您需要做出决定的是有多少用户数据可以保持过时状态,以及哪些数据位需要保持更新。对于需要保持更新的任何内容,例如可能影响路由的用户名,您可以监听核心用户集合的更改:

exports.updateUser = functions.firestore
    .document('user/{userId}')
    .onUpdate((change, context) => {
      const newValue = change.after.data();
      const previousValue = change.before.data();

      const newName = newValue.name;
      const oldName = previousValue.name;

      if (newName !== oldName){
          //update all questions where userId matches.
      }

      // perform other desired operations ...
    });

有关 Cloud Functions 的更多信息,您可以查看文档的这一部分:https://firebase.google.com/docs/functions/firestore-events#trigger_a_function_when_a_document_is_updated

【讨论】:

  • 是的,这是个好主意,但如果您需要更改用户名,是否需要更改每个问题?
  • 是的!我刚刚使用 Firebase Cloud Functions 更新了答案。
【解决方案2】:

嗯,稍微研究一下异步函数已经取得了一些成果。我更改了代码中的一些内容。

可变用户数据变为

用户数据:任意;

在我的 loadItems() 的订阅中,我调用 getUsers()

还有我的getUsers()

getUsers() {    
  for (const item of this.tableData) {
    this.usuarioService.getUserByKey(item.userId).subscribe(response => {
      this.userData[item.userId] = response[0].payload.doc.data();
    });
  }
}

【讨论】:

    猜你喜欢
    • 2020-04-15
    • 1970-01-01
    • 1970-01-01
    • 2020-07-25
    • 2021-10-13
    • 2020-06-27
    • 2021-11-26
    • 2020-07-02
    • 1970-01-01
    相关资源
    最近更新 更多