从可观察的订阅和map()

时间:2018-05-05 21:00:49

标签: javascript firebase rxjs observable google-cloud-firestore

getPlayerFromMatchDayAvailable(player:Player, id:String):string{

  this.playerscollection = this.afs.collection<Player>(`matchdays/${id}/availablePlayers`, ref => ref.where('email','==','john@test.com'))
  this.players2 = this.playerscollection.snapshotChanges()
  this.players2.subscribe( data => {
    if(data){
      console.log(data);
      data.map(test => {
        console.log(test.payload.doc.id); -> **need to return this**
        this.af = test.payload.doc.id;

      });

    }

  });




  console.log(this.af)
  return this.af;



  //  this.players2.forEach(player => {
  //    player.forEach(playerData => {
  //      let data = playerData.payload.doc.data();
  //      let ids = playerData.payload.doc.id;
  //      this.af = playerData.payload.doc.id;
  //      console.log(id,data)

  //    });
  //  });




  }

希望将test.payload.doc.id的值作为字符串返回,以便我可以在component.ts文件中使用它。请告诉我如何从订阅中返回该值。

编辑2

将订阅更改为组件内部,并将已分配的ID更改为字符串变量。 我需要此方法中的id从firestore中的子集合中删除文档。我想我不能为.map()函数中的变量赋值?

  onNotAvailableClick(){
    this.authService.getAuth().subscribe(auth => {
      if(auth){
        this.loggedInUser = auth.email;

        this.playerService.getPlayers().subscribe(players => {
          this.allPlayers = players;
         // console.log(users);

        //  console.log(this.loggedInUser);


        this.myPlayer = this.allPlayers.filter(user => user.email == this.loggedInUser)
        //  console.log(this.myUser);

        this.currentUser = this.myPlayer[0];

        this.p = this.matchdayService.getPlayerFromMatchDayAvailable(this.myPlayer[0],this.id)
        this.p.subscribe( data => {
            if(data){
              console.log(data);
              data.map(test => {
                console.log(test.payload.doc.id);
                this.pid = test.payload.doc.id;

              });

            }

          });

        console.log(this.pid)
        });




      }else{

      }
    });
  }

0 个答案:

没有答案