首页 > 解决方案 > 运行时错误: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());
      });
  }
}

如您所见,我试图以我的方式解决错误。任何可能知道我该如何解决的人?

标签: angularmongodbionic-framework

解决方案


只需从服务返回响应。由于您从组件订阅 observable,因此无需从服务订阅它。

userSignup(review):Observable<any>{

  return this.http.post(this.registerUrl, review);
}

推荐阅读