1

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.


  • You question is unclear, what is the problem and what have you already tried? - mika
  • @mika . Yes, I have tried it but i am new to ionic so I am not able to get what parameters to pass in getUsers() method in login.ts. can you please help? - Raghav
  • What you need to pass to the request depends on the endpoint your sending your request to. So you want to ask the developer of the API. - mika
  • @mika. /user_Login this is the endpoind. means the full API URL. - Raghav

1 답변


1

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;)


  • Can you tell me the what parameters i have to pass because I am familiar with the arguments that i have to pass. - Raghav
  • Or can you tell me the proper method for the API to fetch the details and match the fields from the login page. - Raghav
  • Can you share your login page code? How do you collect credentials? - Sergey Rudenko
  • I have made new method and now i am getting a different error . stackoverflow.com/questions/53664700/… - Raghav

Linked


Related

Latest