Ionic 3 Uncaught (in promise): [object Object]

#1

I am trying to connect a PHP Restful API. I tested the API with Postman and it is working just fine, in order to implement it in Ionic I did the following, I first made a provider named Authservice:

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

  let apiUrl = "http://localhost/api/"
 /*
Generated class for the AuthServiceProvider provider. 

See https://angular.io/guide/dependency-injection for more info on                                                                    
and Angular DI.
*/
@Injectable()
export class AuthServiceProvider {

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

 postData(credentials, type) {
 return new Promise((resolve, reject) => {
  let headers = new HttpHeaders();

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

  }

 }

And a Signup page:

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { AuthServiceProvider } from '../../providers/auth-service/auth-  service';

 @IonicPage()
 @Component({
selector: 'page-signup',
templateUrl: 'signup.html',
})
export class SignupPage {
 responseData: any;
userData = {"username": "","password": "", "name": "","email": ""};
 constructor(public navCtrl: NavController, public authServiceProvider:   AuthServiceProvider) {
}
   signUp() {
this.authServiceProvider.postData(this.userData, "signup").then((result) =>{
 this.responseData = result;
 console.log(this.responseData);
  localStorage.setItem('userData', JSON.stringify(this.responseData));
  });
    }
   goToLogin() {
  this.navCtrl.pop();
       }
       }

When running this I am getting an Uncaught (in promise): [object Object] error as can be seen here https://i.stack.imgur.com/gtYdO.png

#2

import ‘rxjs/add/operator/toPromise’;

Add the above import to your provider

1 Like