如何从firebase中获取JSON数据,然后在angular 6中使用它,firebase返回带有value标签的数据

时间:2018-08-10 13:21:37

标签: firebase google-cloud-functions angular6

我正在使用firebase函数从db获取数据,这就是我的操作方式,

exports.getTopPlayers = (request,response)=> { 
    SavePlayers(function(data,err){
        if(err) console.log(err);
        response.header('Access-Control-Allow-Origin', '*');
        response.header(
            'Access-Control-Allow-Headers',
            'Origin, X-Requested-With, Content-Type, Accept'
          );
        const dbRef = admin.database().ref().child('topplayers/-LISMykRqLrVcc7xrK60');
        dbRef.on('value', snap => {
            var dbPlayer = snap.val();
            response.send(dbPlayer);
        });
    });

然后我在我的角度为6的网站中使用它

getTopPlayers() {
    return this.http.get(this.topPlayerURL);
}

是以下格式的数据,

{value: "[{"name":"WHYALWAYSME","tag":"9P08LYLL","rank":1,"…na":"League 8","arenaID":20,"trophyLimit":6100}}]"}

我想摆脱这个价值标签。我怎么能够?当我尝试使用

  

ngFor(* ngFor =“让topPlayer $的tp返回),无法循环   [object,object]

我想要以下格式的数据,

[
{
name: "Leslie",
tag: "RPP89PVY",
rank: 1,
previousRank: 3,
expLevel: 13,
trophies: 6361,
donationsDelta: null,
clan: {
tag: "9CU2PQ2J",
name: "不正经的养老院",
badge: {
name: "Cherry_Blossom_04",
category: "01_Symbol",
id: 16000131,
image: "https://royaleapi.github.io/cr-api-assets/badges/Cherry_Blossom_04.png"
}
},
arena: {
name: "Grand Champion",
arena: "League 8",
arenaID: 20,
trophyLimit: 6100
}
},

1 个答案:

答案 0 :(得分:0)

我找到了解决方案, 在有角度的组件初始化方法中,我执行了以下操作: 调用服务并读取字符串数组中的数据,

topPlayer$: string[];
ngOnInit() {
    this.topPlayerSrvice.getTopPlayers()
      .subscribe(response => {
          let topPlayer: string[];
          topPlayer =  response.json();
          this.topPlayer$ =  JSON.parse(topPlayer['value']);
      });
  }