Hello friends, first of all im new to Ionic and im working in using Services to pass variables from one form to another, i have a bit of a issue at the moment.
I have a login form, the submit button post the data to my backend api and gives a json in response with error codes and information that i need.
Then with the response i update the variables on my dataService to get all the info i need from the json and use it through all my app.
After i stored all the variables i need to get i alert them just to make sure they are available as you can see in the screen captures im adding.
Then if the login is correct, i go to the MAIN page and in the main page ts i use the ngOnInit to get the variables from my dataService to my main page.
But sometimes i don’t know why the variables apparently are not ready i think? correct me if i’m wrong about that.
How can i get this to work
THANKS IN ADVANCE
Example of my Login TS
Login(){
var hash = require(‘hash.js’);
var pass = hash.sha256().update(this.password).digest(‘hex’);
let urlSearchParams = new URLSearchParams();
urlSearchParams.append(‘idusuario’, this.usuario);
urlSearchParams.append(‘password’, pass);this.GetService.getData(`LOGINPREM?${urlSearchParams.toString()}`) .subscribe(async data => {this.result = JSON.stringify(data); // get data in result variable this.dataService.setjsonresult(this.result); alert(this.dataService.getjsonresult()); this.allData = JSON.parse(this.result); // parse json data and pass json string this.dataService.setuserID(this.allData['Cliente']); alert(this.dataService.getuserID()); this.dataService.setName(this.allData['Nombre']); alert(this.dataService.getName()); this.dataService.setPoints(this.allData['SaldoPuntos']); alert(this.dataService.getPoints()); if (this.allData['Error']== 200) { const toast = await this.toastController.create({ message: "Bienvenido " + this.allData['Nombre'], position: 'top', duration: 2000 }); toast.present(); //this.router.navigate(['/news', {Cliente: this.allData['Cliente']}]); //this.router.navigate(['/welcome', this.allData]); this.router.navigateByUrl('/menu/main'); return false; } else { const toast = await this.toastController.create({ message: "Codigo de Error: " + this.allData['Error'] + " Descripcion: " + this.allData['Descripcion'] , position: 'top', duration: 2000 }); toast.present(); //this.router.navigate(['/home', this.allData]); this.router.navigateByUrl('/login'); return false; } });
}
Example of Main.ts
import { Component, OnInit } from ‘@angular/core’;
import { DataService } from ‘./…/…/services/data.service’;
import { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot } from ‘@angular/router’;@Component({
selector: ‘app-main’,
templateUrl: ‘./main.page.html’,
styleUrls: [‘./main.page.scss’],
})
export class MainPage implements OnInit {
cliente : any;
points: any;
id:any;
constructor(private router: Router, private dataService: DataService) { }ngOnInit() {
this.id = this.dataService.getuserID(); //if(!this.id){ //this.router.navigateByUrl('/login'); //}; this.cliente = this.dataService.getName(); this.points = parseInt(this.dataService.getPoints());
}
}