Hi,
I am learning Ionic/Angular with Firebase and can’t progress further now therefore I’ve came here to ask for help.
Basically I am requesting an array from firebase that contains 2 items and creating an component with that information to present but it keeps duplicating the first item, code below:
perfil.ts
loadExp(){
this.perfilExpKeys = [];
this.db.getExpUser(this.auth.getLoggedUID()).then ((data) => {
this.perfilExp = data.val();
console.log(this.perfilExp);
this.itemsService.getKeys(this.perfilExp).forEach((key) => {
console.log(key);
console.log(this.perfilExpKeys);
this.perfilExpKeys.push(key);
this.pegaExp();
});
});
});
}
pegaExp(){
this.perfilExpKeys.forEach(key => {
this.db.getExpRef().child(key).once('value').then((snap) => {
console.log(snap.val());
this.experiencias.unshift(this.mappingService.getExp(snap.val(), key));
});
});
}
database methods
getExpUser(userUid: string) {
return this.experienciasRef.orderByChild('user/uid').equalTo(userUid).once('value');
}
getExpRef(){
return this.experienciasRef;
}
items service method
getKeys(object): string[] {
return lodash.keysIn(object);
}
mapping method
getExp(snapshot: any, key: string): Expinterface {
let experiencia: Expinterface = {
key: key,
titulo: snapshot.titulo,
detalhes: snapshot.detalhes,
localizacao: {
street: snapshot.localizacao.street,
houseNumber: snapshot.localizacao.houseNumber,
postalCode: snapshot.localizacao.postalCode,
city: snapshot.localizacao.city,
district: snapshot.localizacao.district,
countryName: snapshot.localizacao.countryName,
countryCode: snapshot.localizacao.countryCode,
latEncontrada: snapshot.localizacao.latEncontrada,
lngEncontrada: snapshot.localizacao.lngEncontrada
},
linguas: snapshot.linguas,
preco: snapshot.preco,
dateCreated: snapshot.dateCreated,
user: {
uid: snapshot.user.uid,
username: snapshot.user.username
},
comments: snapshot.comments == null ? 0 : snapshot.comments,
duracao: snapshot.duracao,
categoria: snapshot.categoria
};
return experiencia;
}
logs that it is sending twice the first object of array:
Object {-Kn5OGt8m_2zSSUVRb5b: Object, -Kn5OPCd1R52AA2Y2Dve: Object}
perfil.ts:139 -Kn5OGt8m_2zSSUVRb5b
perfil.ts:140 []
perfil.ts:139 -Kn5OPCd1R52AA2Y2Dve
perfil.ts:140 ["-Kn5OGt8m_2zSSUVRb5b"]
perfil.ts:151 Object {categoria: "beer", dateCreated: "Tue Jun 20 2017 13:45:34 GMT-0300 (-03)", detalhes: "te", duracao: "2", linguas: Array(1)…}
perfil.ts:151 Object {categoria: "beer", dateCreated: "Tue Jun 20 2017 13:45:34 GMT-0300 (-03)", detalhes: "te", duracao: "2", linguas: Array(1)…}
perfil.ts:151 Object {categoria: "beer", dateCreated: "Tue Jun 20 2017 13:46:08 GMT-0300 (-03)", detalhes: "te", duracao: "2", linguas: Array(1)…}
any help would be much appreciated.
firebase scheme:

thanks.