Ionic 2等待多个回复

时间:2017-01-09 02:26:14

标签: javascript angularjs ionic-framework ionic2

我的应用登录页面出现问题。我是Ionic 2和Angular的新手,我试图在Google的帮助下解决这个问题,但到目前为止还没有成功......

这里的这些行导致问题,警报返回"未定义"一旦我点击登录按钮,甚至认为它应该等待响应。

let accessToken = this.getAccessToken();
let details     = this.getProfileDetails(accessToken);
alert(JSON.stringify(details));

整个代码:

import { Component } from '@angular/core';
import { NavController, Platform } from 'ionic-angular';
import { FbProvider } from '../../providers/fb-provider';
import { TabsPage } from '../tabs/tabs';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';

@Component({
  selector: 'page-login',
  templateUrl: 'login.html'
})
export class LoginPage {

  platform
  fb
  email
  name
  id

  constructor(public navCtrl: NavController, pf: Platform, fbProvider: FbProvider, public http: Http) {
    this.platform = pf;
    this.fb = fbProvider;
    this.email = '';
    this.name = '';
    this.id = '';
  }

  ionViewDidLoad() {
    console.log('Hello LoginPage Page');
  }

  fbLogin() {
    let accessToken = this.getAccessToken();
    let details     = this.getProfileDetails(accessToken);
    alert(JSON.stringify(details));
  }

  getAccessToken(){
    this.fb.login().then((fbLoginData) => {
      let params = new FormData();
      params.append('facebookAccessToken', fbLoginData.authResponse.accessToken);
      this.http.post('http://myHostUrl/api/accessToken', params).map(res => res.json())
      .subscribe(
        data => {
          return data.accessToken;
        },err => {
          alert(err);
        }
      );
    },(err) => {
      alert('Facebook login failed');
    });
  }

  getProfileDetails(accessToken){
    let params = new FormData();
    params.append('accessToken', accessToken);
    this.http.post('http://myHostUrl/api/userDetails', params).map(res => res.json())
    .subscribe(
      data => {
        return data;
      },err => {
        alert(err);
      }
    );
  }

}

1 个答案:

答案 0 :(得分:1)

它未定义,因为您需要等待异步函数完成。以下代码用rxjs来管理两个函数和http调用的异步。试试吧。

  fbLogin() {
    this.getAccessToken()
      .switchMap(accessToken => this.getProfileDetails(accessToken))
      .first() // Just one and complete ....
      .subscribe(
        details => alert(JSON.stringify(details)),
        error => alert(error)
      );
  }

  getAccessToken() : Observable<any> {

    return Observable.fromPromise(<Promise<any>> this.fb.login())
      .map(fbLoginData => fbLoginData.authResponse.accessToken)
      .switchMap(accessToken => {
        let params = new FormData();
        params.append('facebookAccessToken', accessToken);
        return this.http.post('http://myHostUrl/api/accessToken', params)
          .map(res => res.json())
          .map(data => data.accessToken)
      });

  }

  getProfileDetails(accessToken) : Observable<any>{

    let params = new FormData();
    params.append('accessToken', accessToken);
    return this.http.post('http://myHostUrl/api/userDetails', params).map(res => res.json());

  }