【发布时间】:2017-01-20 11:33:39
【问题描述】:
我正在努力计算和增加 for 中的数字。我正在使用 Angular 2、typescript、Ionic 2 和 Firebase。
这是交易,我正在从 firebase 查询一些东西,因为它是一个我不能在其上使用 .length 的对象,所以我声明了一个 counter: number = 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;
}
})
}
})
所以它是一个 for 内的 for,因为我需要先获得一个密钥,然后才能获得我需要计数的寄存器。 算法我将这个寄存器保存在一个作为对象的变量中(这是我从代码中获取的部分),我还尝试迭代这个对象并增加它
在我的代码开头:
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();
})
});
})
}
}
【问题讨论】:
-
这是包含计数器的同一个对象吗? ..你能调试并检查一下这是指什么吗?
-
能把MedicoAlertasPage的完整代码放上来吗?
-
@aminarghavani 刚刚编辑,现在它拥有完整的代码
-
你把你的
console.log();放在哪里? -
@aminarghavani inicializaAlertas() 方法
标签: javascript angularjs firebase ionic-framework firebase-realtime-database