首页 > 解决方案 > Javascript推送功能

问题描述

我想创建简单的社交媒体应用程序。我现在正在与组一起工作。但我不能只过滤某些用户是成员的组。代码如下

import { Component, OnInit, OnChanges } from '@angular/core';
import { AngularFireDatabase } from '@angular/fire/database';
import { GroupsService } from '../groups.service';

@Component({
  selector: 'app-groups',
  templateUrl: './groups.component.html',
  styleUrls: ['./groups.component.scss']
})
export class GroupsComponent implements OnInit {
  uid = localStorage.getItem('uid')
  groups: Array<any>;
  mygroups: Array<any>;
  sgroups;
  constructor(private db: AngularFireDatabase, private _groups: GroupsService) {
   }


  ngOnInit() {
    this._groups.getGroups().subscribe((data) => {
      this.groups = data;
    })
    this.loadGroups()
  }
  search(e) {
    this.sgroups = this.groups.find(gr => gr.name.toLowerCase().indexOf(e.target.value.toLowerCase()) > -1)
  }
  loadGroups() {
    this.groups.map(gr => {
     this._groups.getGroupMembers(gr.id).subscribe((data: any) => {
         data.map(mem => {
           if(mem.uid == this.uid) {
            this.mygroups.push(gr); //here is the problem
           }
         })
     })

    })
  }
  scrollnav() {
    document.body.scrollTop = 0;
    document.documentElement.scrollTop = 0;
  }
}

欢迎任何帮助。非常感谢!

标签: javascriptangulardictionary

解决方案


问题在于初始化。初始化 mygroups 像

mygroups: any[] = [];

代替

 mygroups: Array<any>;

推荐阅读