gpt4 book ai didi

multithreading - 代码在从Firebase获取数据之前////Angular 2

转载 作者:行者123 更新时间:2023-12-03 13:09:25 25 4
gpt4 key购买 nike

我尝试从firebase及其成功中获取数据,但需要花费几秒钟的时间,
代码继续,因此变量的值为null。
为什么会这样呢?它是线程的吗?我该怎么办?

gameslist.component.ts

        import {Component} from 'angular2/core';
import {Game} from './Game'
import {NgStyle} from "angular2/common";
import {DataService} from "./data.service";

declare var firebase: any;

@Component({
selector:'gameslist',
templateUrl: 'app/ts/gameslist.component.html',
styleUrls: ['app/ts/gameslist.component.css'],
inputs: ['games'],
directives: [NgStyle],
providers:[DataService]

})

export class gamesListComponent{
public games;
public currentGame=[];
public votes1: number;
constructor(private dataService:DataService){ this.votes1=0;this.currentGame=[];}

fbPostVote(id_game:number,ChooseVote:number){
this.votes1=this.dataService.getGameById(id_game); //Here return null
this.votes1++;
var updates = {};
updates['/games/'+id_game+'/rec_1_votes'] = this.votes1;
updates['/games/'+id_game+'/rec_1_color'] = 'pink';
this.games[id_game].rec_1_color = 'pink';
return firebase.database().ref().update(updates);
}

data.service.ts
       import {Injectable} from 'angular2/core';
import {Http, Response} from 'angular2/http';
import 'rxjs/Rx';

@Injectable()
export class DataService{

constructor(private http:Http){ }

getGameById(id_game:number){
firebase.database().ref('/games/'+id_game+'/rec_1_votes').on('value',function(snap) {
return snap.val();
});
}
}

谢谢。

最佳答案

getGameById()不返回任何内容。更改为

    getGameById(id_game:number){
return firebase.database().ref('/games/'+id_game+'/rec_1_votes').on('value',function(snap) {
return snap.val();
});
}

关于multithreading - 代码在从Firebase获取数据之前////Angular 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40008888/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com