I can not print the object's attribute in the log

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, I try to print any attribute of the user, 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); 
  })
  })
  }

This is the output:

butifyoutrytoprintthewholeobjectitappearsnormally:

    
asked by anonymous 15.08.2017 / 15:27

2 answers

1

The problem is that you are waiting for the return that is not yet ready. Your getUserKeyMAC function will not be able to pass the return you expect at the desired time because it is asynchronous. This means that the following lines will be processed without waiting for the other:

this.items = this.angularFire.list('/Usuarios', { preserveSnapshot: true });
us = new Usuario
this.items.subscribe(snapshots => {// códigos aqui});
return us;

With Angular and this new architecture of asynchronous calls you will have to change the thinking a little whenever you make an HTTP connection or something that returns an Observable, Subscribe, etc. So, never expect the return at the time of the call.

See a concept of how you could adapt.

getUserKeyMAC(MAC: string): Observable<Usuario[]>{

  // outras coisas aqui 

   this.items = this.angularFire.list('/Usuarios', { preserveSnapshot: true });

   return this.items.map(snapshot => {

    let us = new Usuario();
    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
  });
})

}

markInput (obs: Observable) {

// The method call would look like this

obs.subscribe(macs => {
  macs.forEach((mac) => {

      let us:any;
      us = new Usuario;
      let resultObservable =this.hfb.getUserKeyMAC(String(mac.mac));
      resultObservable.subscribe(usuarios => {

             if (usuarios.lenght > 0) 
             { 
             }
       }, (error)=> { console.log('ocorreu um erro', error); });
   }
}

I will not say that this is simple, it is not simple, it took me a while to get the idea, but you will need to adapt to those types of calls.

And the question that stays in the air is, why printing the object in console.log worked? My theory is as follows, you printed an object while the call was active and, as object, the properties could be very well changed throughout the processing, and that's exactly what happened. To prove this theory, instead of using console.log (us), you can use console.log (JSON.stringify (us)). This will tell you that the object is empty.

If you need help adapting, post here, or create an example in the plunker, it's easier to help.

    
15.08.2017 / 17:11
0

Hello, good afternoon.

Have you tried using it that way?

Class


export class Usuario{
    private _id : string;
    private _bluetoothMAC: string;
    private _cpf: string;

//gets and sets

get id():string {

    return this._id;

}

set id (id:string) {

    this._id = id

}

Function that directs the attributes


us = new Usuario;
us.cpf = "000.000.000.00";
us.id = "13449483890";
us.bluetoothMAC = "...";

User Class User Console



console.log(us.id);

    
15.08.2017 / 16:21