1
Well, I have a class:
export class Usuario{
private id : string;
private bluetoothMAC: string;
private cpf: string;
private nome: string;
private oficinaVisitadas: Oficina[];
private cacheMAC: Mac[];
private saidaMAC: Mac[];
constructor(){
}
//gets and sets
I have this method that returns the user
getUserKeyMAC(MAC: string):Usuario{
let key: string = ""
let oMAC: string = ""
let rt: any;
let us:any;
oMAC = MAC
this.items = this.angularFire.list('/Usuarios', { preserveSnapshot: true });
us = new Usuario
this.items.subscribe(snapshots => {
snapshots.forEach(snapshot => {
let arrayTemp:any
let aMAC: string = ""
aMAC = snapshot.val().bluetoothMAC
if (aMAC === oMAC) {
us.$id=snapshot.key
us.$nome = snapshot.val().nome;
us.$cpf = snapshot.val().cpf;
us.$bluetoothMAC = snapshot.val().bluetoothMAC;
arrayTemp= new Array;
arrayTemp = snapshot.val().oficinaVisitadas
us.$oficinaVisitadas=arrayTemp
}
});
})
return us
}
and here where I get the return,try to print any user attribute,but it comes as Undefined:
marcaEntrada(obs:Observable<Mac[]>){
obs.subscribe(macs=>{
macs.forEach((mac)=>{
let us:any;
us = new Usuario;
us=this.hfb.getUserKeyMAC(String(mac.mac));
console.log(us.$id);
})
})
}
but if I try to print the whole object it appears normally:
I got it,I was trying to avoid using the Observable after setting the user, as I will do various validations and updates within firebase based on the returned user,the basics of this angular module and insert the schedule in which the mac was captured and update until last seen,this will require a lot of code within the subscribe,sera it has problem?
– Kai
I don’t see any problems, but I suggest you, instead of putting all the code inside the subscribe, call the method inside the subscribe. This way the code becomes more readable. Example: observable.subscribe(us=> this.trataUsuario(us));
– Gilberto Alexandre
But in this case the object would be populated or controlled by the observable still?
– Kai
It would already be the populated object. But in this situation it would be equal to the example code that I posted last (the resultObservable.subscribe), which is when the object is already outside observable
– Gilberto Alexandre
Typeerror: resultObservable.subscribe is not a Function
– Kai
The use of the suggested instruction will depend greatly on the object contained in this.items. Therefore, the ideial to assist you would be to create a case in the plunker. If you are using angular lib/angularfire2 the type of items will be items: Firebaselistobservable<any[]>, as in the home page example. Perhaps, some rxjs package such as Observable, Operator, Subscriber or Subscription is missing. The safest is for you to investigate (console.log) what type of items and methods it contains. Package import: import { <pack_nm> } from 'rxjs/<pack_nm>';.
– Gilberto Alexandre