JSON error is coming while performing login using API in Ionic

Viewed 172

I am performing the login using API in Ionic but I am getting the error :

Error: Property 'json' does not exist on type '{}'.

This is my loginpage.html:

<ion-header>

  <ion-navbar>
    <ion-title>loginpage</ion-title>
  </ion-navbar>

</ion-header>


<ion-content padding>
  <form (submit)="getloginUsers()">
    <ion-list>

      <ion-item>
        <ion-label fixed>Email</ion-label>
        <ion-input type="email" [(ngModel)]="userData.email" name="email"></ion-input>
      </ion-item>

      <ion-item>
        <ion-label fixed>Password</ion-label>
        <ion-input type="password" [(ngModel)]="userData.password" name="password"></ion-input>
      </ion-item>

      <div padding>
        <button ion-button color="primary" block>Login</button>
      </div>

    </ion-list>
  </form>
</ion-content>

This is my loginpage.ts:

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { RestapiProvider } from '../../providers/restapi/restapi';
import { ListPage } from '../list/list';

@IonicPage()
@Component({
  selector: 'page-loginpage',
  templateUrl: 'loginpage.html',
})
export class LoginpagePage {
  responseData : any;
  userData = {"email": "", "password": ""};
  constructor(public navCtrl: NavController, public navParams: NavParams,
    public restProvider: RestapiProvider) {
      this.getloginUsers();
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad LoginpagePage');
  }

    getloginUsers(){
    this.restProvider.getUsers(this.userData,'user_Login').then((result) => {
      if(result){
       this.responseData = result.json();
     if(this.responseData.userData){
     console.log(this.responseData);
     console.log("User Details");
     this.navCtrl.push(ListPage);
     }
     else{
       console.log("Incorrect Details"); }
    }
     }
     , (err) => {
     // Error log
   });

 }
}

This is code this.responseData = result.json(); error is coming.

Error: Property 'json' does not exist on type '{}'.

This is my Service restapi.ts:

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { HttpHeaders } from '@angular/common/http';
import { Response } from '@angular/http';
import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/timeout'

let apiUrl = 'http://192.168.1.10/honeybee/HoneyApi/';

@Injectable()
export class RestapiProvider {

  token:any;

  constructor(public http: HttpClient) {
    console.log('Hello RestapiProvider Provider');
  }


  getUsers(credentials, type) {
    return new Promise((resolve, reject) => {
      var headers = new HttpHeaders();
      headers.append('Access-Control-Allow-Origin' , '*');
      headers.append('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT');
      headers.append('Accept','application/json');
      headers.append('Content-Type','application/json');

  this.http.post(apiUrl + type, JSON.stringify(credentials), {headers: headers})
    .subscribe((res: Response) => {
        resolve(res);
      }, (err) => {
          reject(err);
        });
    });
  }
}

I have included the FormsModule in app.module.ts. Any help is much appreciated.

1 Answers

Since you are using HttpClient, you dont have to generally use result.json();

 this.responseData = result;

Also you do not have to use Promise, change the service code as follows,

 getUsers(credentials, type) {
      var headers = new HttpHeaders();
      headers.append('Access-Control-Allow-Origin' , '*');
      headers.append('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT');
      headers.append('Accept','application/json');
      headers.append('Content-Type','application/json');
      return this.http.post(apiUrl + type, JSON.stringify(credentials), {headers: headers});
  }

and in your component,

this.restProvider.getUsers(this.userData,'user_Login').subscribe((data) => {
    console.log(data);
});
Related