How to call variables from method

import { Component, OnInit } from ‘@angular/core’;
import { HomePage } from ‘./…/home/home.page’;
import { HttpClient } from ‘@angular/common/http’;
import { ToastController, AlertController, MenuController } from ‘@ionic/angular’;
import { Storage } from ‘@ionic/Storage’;
import { Router } from ‘@angular/router’;
import { Push, PushObject, PushOptions } from ‘@ionic-native/push/ngx’;
import { Device } from ‘@ionic-native/device/ngx’;

@Component({
selector: ‘app-giris’,
templateUrl: ‘./giris.page.html’,
styleUrls: [’./giris.page.scss’],
})
export class GirisPage implements OnInit {
userdata: any;
regID: any;
pushregid: any;
dbregID: any;
deviceid: any;
devmodel: any;
devserial: any;
devversion: any;
dplatform: any;

constructor(
private http: HttpClient,
public toastController: ToastController,
private storage: Storage,
public alertCtrl: AlertController,
private router: Router,
private menuCtrl: MenuController,
private push: Push,
private device: Device) {
}

ngOnInit() {
console.log(‘ngOnInit GirisPage’);
this.menuCtrl.swipeEnable(false);
this.menuCtrl.enable(false);
this.logForm();
}

data = {}
apiUrl=‘http://192.168.1.91:8080/mobilbks/’;
logForm() {
let newData = {
funcion: ‘logForm’,
data: this.data
}
//console.log(this.data);
this.http.post(this.apiUrl, JSON.stringify(newData)).subscribe(data => {
if (data[‘success’] === “Login Success”) {
this.storage.set(‘session_storage’, data[‘res’]);
let newDevice = {
funcion: ‘newDevice’,
data: this.data,
deviceID: this.device.uuid,
devmodel: this.device.model,
devserial: this.device.serial,
devversion: this.device.version,
dplatform: this.device.platform,
dbregID: this.pushSetup()
}
console.log(newDevice);
this.http.post(this.apiUrl, JSON.stringify(newDevice)).subscribe(data => {
this.deviceid = data;
console.log(this.deviceid);
});
this.router.navigateByUrl(‘menu’);
} else {
this.girishataAlert();
this.router.navigateByUrl(‘giris’);
}
},
(err) => {
console.log(err);
});
}

async girishataAlert() {
const alert = await this.alertCtrl.create({
header: ‘Hatalı Giriş’,
message: ‘Lütfen Kullanıcı Bilgilerinizi Kontrol Ediniz’,
buttons: [‘Tamam’]
});
await alert.present();
}

pushSetup() {
const options: PushOptions = {
android: {
senderID: ‘11111111111111’
},
ios: {
alert: ‘true’,
badge: true,
sound: ‘false’
}
};

const pushObject: PushObject = this.push.init(options);
pushObject.on('registration').subscribe((data: any) => {
  //alert('Device token/handle is: ' + data.registrationId);
      this.regID = **data.registrationId**;

});
}

}

Hi, I want save the device registrationID (this.regID) to mysql database.
but I can’t call the method

please help me

Hi,

could you please put the whole code in a code section to make it more readable.

And eventually you should read more about services. That what you want to do, from my understanding, is very easy to achive with a service.
You can make a restapi serivice and inject it in your component, from there you will be able to call variables or methods from that service.

https://angular.io/tutorial/toh-pt4

import { Component, OnInit } from '@angular/core';
import { HomePage } from './../home/home.page';
import { HttpClient } from '@angular/common/http';
import { ToastController, AlertController, MenuController } from '@ionic/angular';
import { Storage } from '@ionic/Storage';
import { Router } from '@angular/router';
import { Push, PushObject, PushOptions } from '@ionic-native/push/ngx';
import { Device } from '@ionic-native/device/ngx';

@Component({
  selector: 'app-giris',
  templateUrl: './giris.page.html',
  styleUrls: ['./giris.page.scss'],
})
export class GirisPage implements OnInit {
  userdata: any;
  regID: any;
  pushregid: any;
  dbregID: any;
  deviceid: any;
  devmodel: any;
  devserial: any;
  devversion: any;
  dplatform: any;

  constructor(
    private http: HttpClient,
    public toastController: ToastController,
    private storage: Storage,
    public alertCtrl: AlertController,
    private router: Router,
    private menuCtrl: MenuController,
    private push: Push,
    private device: Device) {
  }


  ngOnInit() {
    console.log('ngOnInit GirisPage');
    this.menuCtrl.swipeEnable(false);
    this.menuCtrl.enable(false);
    alert(this.pushSetup());
    this.logForm();
  }

  data = {}
  apiUrl='http://192.168.1.91:8080/mobilbks/';
  logForm() {
    let newData = {
      funcion: 'logForm',
      data: this.data
    }
    this.http.post(this.apiUrl, JSON.stringify(newData)).subscribe(data => {
      if (data['success'] === "Login Success") {
        this.storage.set('session_storage', data['res']);
        let newDevice = {
          funcion: 'newDevice',
          data: this.data,
          deviceID: this.device.uuid,
          devmodel: this.device.model,
          devserial: this.device.serial,
          devversion: this.device.version,
          dplatform: this.device.platform,
          dbregID: 
        }
        console.log(newDevice);
        this.http.post(this.apiUrl, JSON.stringify(newDevice)).subscribe(data => {
          this.deviceid = data;
        });
        this.router.navigateByUrl('menu');
      } else {
        this.girishataAlert();
        this.router.navigateByUrl('giris');
      }
    },
      (err) => {
        console.log(err);
      });
  }

  async girishataAlert() {
    const alert = await this.alertCtrl.create({
      header: 'Hatalı Giriş',
      message: 'Lütfen Kullanıcı Bilgilerinizi Kontrol Ediniz',
      buttons: ['Tamam']
    });
    await alert.present();
  }

  pushSetup() {
    const options: PushOptions = {
      android: {
        senderID: '1111111111111111'
      },
      ios: {
        alert: 'true',
        badge: true,
        sound: 'false'
      }
    };

    const pushObject: PushObject = this.push.init(options);
    pushObject.on('registration').subscribe((data: any) => {
          this.regID = data.registrationId.toString();
  });
  return this.regID;
  }

}

I want to return this.regID as a string to logform() method

declar data = {} as public and push this.regID into it.

sorry but the way you’ve implemented the whole thing is not good. You have functionalities which are logicaly not related to each other and putting them all together in one file makes it hard to debug.
In your case I would make 3 services, one for api calls, second for push notifications and third one is for alerts and toasts. The third one is a matter of taste i guess :slight_smile:
You would probably use an alert or toast more than once so its worth it put it in a service.