angular - 运行时错误:this.userprovider.userSignup(...).subscribe 不是 Ionic 中的函数
问题描述
下面是signup.ts
我想在 MongoDB 中插入数据。我收到类似的错误
this.userprovider.userSignup(...).subscribe 不是函数。
请帮我解决错误
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);
});
}
}
下面是provider/user.ts 这个文件是provider 文件。我在这里使用订阅。但它会产生问题..那么解决方案是什么
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());
});
}
}
如您所见,我试图以我的方式解决错误。任何可能知道我该如何解决的人?
解决方案
只需从服务返回响应。由于您从组件订阅 observable,因此无需从服务订阅它。
userSignup(review):Observable<any>{
return this.http.post(this.registerUrl, review);
}
推荐阅读
- python-3.x - 有没有办法在 Python3 中将列表分组为子列表?
- php - 如何验证记录更新中的唯一字段
- javascript - 通过添加和拖动动态点(闪烁)进行多边形奇怪的重绘
- javascript - handlebars.js 中带糖的条件行
- python - 为什么 len([1,[2,[3]]]]) 返回 2,而 len([1,[2]]) 也返回 2?
- python - 如何将逻辑公式转换为数组并计算?
- string - 在 AWK 中搜索多个字符串
- c# - 无法映射属性“PropertyName”,因为它属于“列表”类型
' - php - WordPress 上带有 SMTP 服务器的 phpmailer:带有 spamcore 8.5/10 的邮件无法到达 Gmail 或 Hotmail
- python - discord.py 中的打字机效果(我用谷歌翻译问这个问题,如有错误请见谅)