【问题标题】:Return from function doesn’t work (ionic)从函数返回不起作用(离子)
【发布时间】:2019-11-19 00:21:32
【问题描述】:

我试图从这个 http.get 获得响应

getChatId(emailTo): any {
    var email = emailTo

    const httpOptions = {
      headers: new HttpHeaders({
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'Token': this.token_value
      })
    };

    this.httpClient.get("https://xxxx=" + email, httpOptions)
      .subscribe(data => {
        console.log(data['_body']);
        return data
      }, error => {
        console.log(error);
        return error
      });
  }

这个在我的构造函数中

this.getChatId(this.emailTo).then((date) => {

      var docRef = firebase.firestore().collection("xxx").doc(date.response);

      docRef.onSnapshot((doc) => {
        this.document = doc.data()

        let chats_message = [];
        for (let k in this.document.messages) {
          chats_message.push(this.document.messages[k]);
        }
        chats_message.sort(function (a, b) { return a.id - b.id; })
        this.messages_chat = chats_message;
        this.content.scrollToBottom(300);//300ms animation speed

        console.log("Array", this.messages_chat);
      })
    });

但它给了我这个错误:

vendor.js:1823 ERROR 错误:未捕获(承诺中):TypeError:不能 读取未定义类型错误的属性“订阅”:无法读取属性 未定义的“订阅”

【问题讨论】:

  • 你在一个不是承诺的函数上使用了 .then。

标签: ionic-framework ionic2 ionic3 ionic4 ionic-native


【解决方案1】:

您应该将您的函数重写为 Observable 以与 httpclient 交互。最好在像 ChatService 这样的服务文件中。您可以使用模型或您接收或发送的任何类型来设计 http 请求。

export class ChatService {

  constructor(private http: HttpClient) {}

  getChatId(emailTo: string): Observable<any> {
    return this.httpClient.get<any>("https://xxxx=/" + email);
  }

}

在构造函数中注入服务的页面调用http请求。

constructor(private chatService: ChatService) {}

getChatId() {
  this.chatService.getChatId(this.emailTo).subscribe(
    result => {
      // do something with result
    },
    error => {
      // do something with error
    }
  );
}

编辑

如果您使用模型在 http 请求中传递和接收数据,您可以将它们定义为类型。 https://blog.angular-university.io/angular-http/

import { User } from '../models/user';

export class ChatService {

  constructor(private http: HttpClient) {}

  getChatId(emailTo: string): Observable<User> {
    return this.httpClient.get<User>("https://xxxx=/" + email);
  }

}

【讨论】:

    【解决方案2】:

    Subscribe 不是 httpclient 在请求时的一个函数。请按照以下代码进行操作

    import { Component } from '@angular/core';
    import { Observable } from 'rxjs/Observable';
    import { HttpClient } from '@angular/common/http';
     
    @IonicPage()
    @Component({
      selector: 'page-sample',
      templateUrl: 'sample.html',
    })
    export class SamplePage {
      sampleDatas: Observable<any>;
     
      constructor(public navCtrl: NavController, public httpClient: HttpClient) { 
        this.films = this.httpClient.get('https://swapi.co/api/films');
        this.sampleDatas
        .subscribe(data => {
          console.log('my data: ', data);
        })
      }

    【讨论】:

      猜你喜欢
      • 2012-10-23
      • 2017-08-08
      • 1970-01-01
      • 2015-05-31
      • 2021-09-30
      • 1970-01-01
      • 2010-12-16
      • 2017-09-03
      • 1970-01-01
      相关资源
      最近更新 更多