Slvr
Slvr

Reputation: 40

Ionic 3 undefined but isn't undefined

My code is taking data from another controller like this:

In the function that i send the info from the controller 1 is

setAhorcado(){ this.navCtrl.push(AhorcadoPage,{itemMsg:this.itemMsg}) }

And in the other i receive normally and set the variable

itemMsg: any;

And in the constructor

this.itemMsg = this.navParams.get('itemMsg');

And... when i did a console.log the variable (in ionViewDidLoad()) is printed two times from the same line and the first time says undefined, and after that is the object.

Can you help me guys? Another problem is when i receive the object and i need an attribute like

this.nombres = this.itemMsg.pass[0].value
console.log(this.nombres.length());

Cannot read the length

Check it:

Undefined and same line (console.log) defined

The code when i receive the object from the 2nd controller is:

@IonicPage()
@Component({
  selector: 'page-ahorcado',
  templateUrl: 'ahorcado.html',
})
export class AhorcadoPage {


  itemMsg: any = this.itemMsg;
  letra: string = '';
  nombres: any = ['ferrocarril'];
  nombreSecreto: any = this.palabraAleatoria(0, (this.nombres.length - 1));
  palabra: any = '';
  muestraHuecos: any = this.muestraHuecosPalabra();
  mensaje: string = '';
  letras_utilizadas: string = '';
  nombresecretomostrar: string = '';

  vidas: number = 6;
  ganador: number = 0;
  imagen: number = 1;

  durationMessages: number = 3000;

  // Creamos un array para guardar las letras que se van seleccionando.
  controlLetras = new Array;

  private callback: Function;

  constructor(public navCtrl: NavController, public navParams: NavParams, private toastCtrl: ToastController,
              public alertCtrl: AlertController) {
    this.itemMsg = this.navParams.get('itemMsg');
    console.log(this.itemMsg);
  }

And in the first is the push:

setAhorcado(){
  this.navCtrl.setRoot(AhorcadoPage);
  this.navCtrl.push(AhorcadoPage,{itemMsg:this.itemMsg})
}

Upvotes: 0

Views: 108

Answers (1)

Najam Us Saqib
Najam Us Saqib

Reputation: 3404

this is happening because your are using navCtrl 2 times.

setAhorcado(){
  this.navCtrl.setRoot(AhorcadoPage);
  this.navCtrl.push(AhorcadoPage,{itemMsg:this.itemMsg})
}

either you use:

setAhorcado(){
  this.navCtrl.setRoot(AhorcadoPage,{itemMsg:this.itemMsg});
}

or this:

setAhorcado(){
  this.navCtrl.push(AhorcadoPage,{itemMsg:this.itemMsg})
}

Your are using navCtrl 2 time with setRoot() and push(). use any one of them.

Upvotes: 1

Related Questions