Angular 2增量计数器不工作

时间:2017-01-19 14:37:51

标签: javascript angularjs firebase ionic-framework firebase-realtime-database

我正在计算和增加一个数字中的数字。我使用的是Angular 2,typescript,Ionic 2和Firebase。

这是交易,我从firebase查询了一些东西,因为它是一个我无法使用 .length 的对象所以我声明了计数器:数字= 0; 位于我的代码顶部和我的 snapshot.val()里面我正在递增它,但最后我做了一个 console.log 0

这是我的代码(我已经拿走了无用的代码在这里发布):

firebase.database().ref('MedicoPacientes/' + id).once('value', snapshot => {
   for (var key in snapshot.val()) {
                    //PEGA TUDO DENTRO DO HISTORICO DE UM USUARIO QUE SEJA TRUE
                    historico.child(key).orderByChild('permissoes').equalTo(true).once('value', snap => {
      for (var key in snap.val()) {
                            this.counter++;
                            //also tryed this.counter += 1 and this.counter = this.counter +1;
                        }
                    });

                    historico.child(key).orderByChild('permissoes/' + cpf).equalTo(id).once('value', snap => {
                        for (var key in snap.val()) {
                            this.counter++;
                            //also tryed this.counter += 1 and this.counter = this.counter +1;
                        }
                    })
                }
            })

因此,我需要在获取需要计数的寄存器之前获取密钥。 Algo我将这个寄存器保存在一个作为对象的变量中(它是我从代码中获取的部分,以便在这里发布)并且我还尝试迭代这个对象并递增它

在我的代码的开头我有:

export class MedicoAlertasPage {
  counter: number = 0;

有人知道我做错了吗?

编辑1

这是完整的页面代码:

export class MedicoAlertasPage {

paciente: any;
semHistorico: boolean;
limit: any;
counter: number = 0;
meuId: any;
contador: number = 15;

alertas: any[] = [];
alertasFiltrado: any[];

constructor(public navCtrl: NavController, public storage: Storage, public loading: LoadingController) {
}

//COPY THE VALUES TO USE IN THE FILTER
inicializaAlertas() {
    this.alertasFiltrado = this.alertas;
}

//FILTER ON TYPING
filtraAcompanhamentos(ev: any) {
    this.inicializaAlertas();

    // set val to the value of the searchbar
    let val = ev.target.value;

    // if the value is an empty string don't filter the items
    if (val && val.trim() != '') {
        this.alertasFiltrado = this.alertasFiltrado.filter((item) => {
            return (item.nome.toLowerCase().indexOf(val.toLowerCase()) > -1);
        })
    }
}

//LOAD USER HISTORY
ionViewWillEnter() {
    let l = this.loading.create({ content: "Aguarde..." });
    l.present();

    this.storage.get('id').then(id => {
        this.storage.get('cpf').then(cpf => {
            let historico = firebase.database().ref('/Historico/');
            firebase.database().ref('MedicoPacientes/' + id).once('value', snapshot => {
                for (var key in snapshot.val()) {
                    //GET ALL THE DATA THAT IS TRUE
                    historico.child(key).orderByChild('permissoes').equalTo(true).once('value', snap => {
                        for (var key in snap.val()) {
                            this.counter++;
                            this.alertas.unshift({
                                chave: key,
                                data: Moment.unix(Number(key)).format('DD/MM/YYYY'),
                                hora: Moment.unix(Number(key)).format('HH:mm'),
                                descricao: snap.val()[key].descricao,
                                paciente: snap.val()[key].nome
                            });
                        }
                    });

                    //GET ALL DATA OF A SELECTED USER
                    historico.child(key).orderByChild('permissoes/' + cpf).equalTo(id).once('value', snap => {
                        for (var key in snap.val()) {
                            this.counter++;
                            this.alertas.unshift({
                                chave: key,
                                data: Moment.unix(Number(key)).format('DD/MM/YYYY'),
                                hora: Moment.unix(Number(key)).format('HH:mm'),
                                descricao: snap.val()[key].descricao,
                                paciente: snap.val()[key].nome
                            });
                        }
                    })
                }
                this.inicializaAlertas();
                l.dismiss();
            })
        });
    })

}

}

1 个答案:

答案 0 :(得分:0)

将您的代码更改为:

ionViewWillEnter() {
    let l = this.loading.create({ content: "Aguarde..." });
    l.present();

    this.storage.get('id').then(id => {
        this.storage.get('cpf').then(cpf => {
            let historico = firebase.database().ref('/Historico/');
            firebase.database().ref('MedicoPacientes/' + id).once('value', snapshot => {
                for (var key in snapshot.val()) {
                    //GET ALL THE DATA THAT IS TRUE
                    historico.child(key).orderByChild('permissoes').equalTo(true).once('value', snap => {
                        for (var key in snap.val()) {
                            this.counter++;
                            this.alertas.unshift({
                                chave: key,
                                data: Moment.unix(Number(key)).format('DD/MM/YYYY'),
                                hora: Moment.unix(Number(key)).format('HH:mm'),
                                descricao: snap.val()[key].descricao,
                                paciente: snap.val()[key].nome
                            });
                        }
                        //GET ALL DATA OF A SELECTED USER
                        historico.child(key).orderByChild('permissoes/' + cpf).equalTo(id).once('value', snap => {
                            for (var key in snap.val()) {
                                this.counter++;
                                this.alertas.unshift({
                                    chave: key,
                                    data: Moment.unix(Number(key)).format('DD/MM/YYYY'),
                                    hora: Moment.unix(Number(key)).format('HH:mm'),
                                    descricao: snap.val()[key].descricao,
                                    paciente: snap.val()[key].nome
                                });
                            }
                            this.inicializaAlertas();
                            l.dismiss();
                        })

                    });


                }

            })
        });
    })

}