I have a API for the login but i am new to Ionic and not able to make correct method for the login.
This is my service: providers/restapi/restapi.ts
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
/*
Generated class for the RestapiProvider provider.
*/
@Injectable()
export class RestapiProvider {
apiUrl = 'http://192.168.1.10/honeybee/register';
constructor(public http: HttpClient) {
console.log('Hello RestapiProvider Provider');
}
getUsers(endpoint: string, body: any, reqOpts?: any) {
return this.http.post(this.apiUrl+'/user_Login' + endpoint, body, reqOpts);
}
}
This is my login.ts
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { RestapiProvider } from '../../providers/restapi/restapi';
/**
* Generated class for the LoginpagePage page.
*
* See https://ionicframework.com/docs/components/#navigation for more info.
* Ionic pages and navigation.
*/
@IonicPage()
@Component({
selector: 'page-loginpage',
templateUrl: 'loginpage.html',
})
export class LoginpagePage {
users: any;
constructor(public navCtrl: NavController, public navParams: NavParams,
public restProvider: RestapiProvider) {
this.getloginUsers();
}
ionViewDidLoad() {
console.log('ionViewDidLoad LoginpagePage');
}
getloginUsers() {
this.restProvider.getUsers()
.then(data => {
this.users = data;
console.log(this.users);
});
}
}
I am not able to pass the parameters in the getUsers() method because I am new to ionic. Please help me for the correct method.
it seems based on your provider you are using Angular 4.3+ with HttpClient that relies on Observables. See here more details how to properly implement calls: https://angular.io/guide/http
In your case you have this method getUsers() in your provider:
getUsers(endpoint: string, body: any, reqOpts?: any) {
return this.http.post(this.apiUrl+'/user_Login' + endpoint, body, reqOpts);
}
It returns Observable, and you have to subscribe to it to make it "act" (do request for you).
So in your component code you should have something like:
getloginUsers() {
this.restProvider.getUsers().subscribe(
(data) => {
this.users = data;
},
(error) => {
console.log(error);
})
}
Also your getUsers method requires a lot of arguments (endpoint, body etc). Please don't forget to pass those;)