Runtime Error: this.userprovider.userSignup(…).subscribe is not a function in Ionic
Runtime Error: this.userprovider.userSignup(…).subscribe is not a function in Ionic
Below is signup.ts
I wanted to insert the data in MongoDB.
I'm getting the error like
this.userprovider.userSignup(...).subscribe is not a function.
Please help me to solve the error
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ViewController } from 'ionic-
angular';
import { UserProvider } from '../../providers/user/user';
@IonicPage()
@Component({
selector: 'page-signup',
templateUrl: 'signup.html',
})
export class SignupPage {
private registerUrl = 'http://localhost:8080/signup';
username: string;
email: string;
password: string;
repassword: string;
constructor(public navCtrl: NavController, public navParams: NavParams, public viewCtrl: ViewController, public userprovider: UserProvider) {
}
signup(): void {
let review = {
username: this.username,
email: this.email,
password: this.password,
repassword: this.repassword
};
console.log(review);
this.userprovider.userSignup(review).subscribe(
res => {
console.log(res);
},
error => {
//this.loading = false;
console.log(error);
});
}
}
Below is provider/user.ts
this file is provider file. I used subscribe here. but it creates problems..so what is the solution
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/catch';
import { BehaviorSubject } from 'rxjs/Rx';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class UserProvider {
data: any;
private registerUrl = 'http://localhost:8080/signup';
constructor(public http: Http) {
this.data = null;
}
userSignup(review): any {
return this.http.post(this.registerUrl, review)
.subscribe(res => {
console.log(res.json());
});
}
}
As you can see I have tried to solve the error in my way.Anyone that maybe knows how I can solve this?
2 Answers
2
Just return the response from the service. Since you are subscribing to the observable from the component, No need to subscribe it from the service.
userSignup(review):Observable<any>{
return this.http.post(this.registerUrl, review);
}
thanks.above error is solved but i have another question is how to insert the data in mongodb ..i have server.ts ready ..
– S.A.
2 days ago
There are two issues here,
(i) You can subscribe only to an observable, change your return type as,
userSignup(review):Observable<any>
(ii) You should use subscribe only within your component, not inside the service method. So change your method as,
userSignup(review):Observable<any>{
return this.http.post(this.registerUrl, review);
}
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
sajeetharan's answer explain the reason
– startup-developer
2 days ago