Using promise with observable in ionic 2 applications

RHUL

I am trying to call a web service in ionic 2 application but getting struck in between promise and observable. This is my function which i am calling on click of a button-

getUserDetailsIfAccessTokenIsSuccess()


{
    this.remedyService.userGetDetail(this.loginDetailsObj.username).subscribe(
      data =>{
        console.log("userGetDetail Success="+JSON.stringify(data));
      },
      err =>{
        console.log("userGetDetail Success="+JSON.stringify(error));
        },
       () =>{
              console.log("Get Userdetails Completed");
      });
}

Now i am calling userGetDetail function in my service file.

userGetDetail(eid){
    var url = '/userGetDetail';
    var params = {
                'EID':eid
            };
    console.log(JSON.stringify(params));
    var headers = new Headers();
    this.createAuthorizationHeader(headers);
    this.storage.get('remedyAccessToken').then((value)=>{
        var token  = value.toString();
        console.log("TOKEN IN REMEDYSERVICES="+token);
        headers.append('Authorization', token);
        console.log("header value inside="+JSON.stringify(headers));
    })
    console.log("header value outside="+JSON.stringify(headers));
    let options = new RequestOptions({ headers: headers });
    this.response = this.http.post(url,this.urlEncode(params),options).map(res => res.json());
    console.log("Response JSON="+JSON.stringify(this.response));
    return this.response;
}

Now the issue is that before Authorization is getting appended to header the flow is going to next line and web api is giving error. Is there anyway i am can synchronise in such a way that after Authorization is appended then only service call should happens. I tried putting the web api call inside then block but it makes the service calls as a promise. Any help will be appreciated.

RHUL

The correct way to call an observer inside a promise and the return an observer will be like this-

return Observable.fromPromise(this.storage.get('remedyAccessToken')).then((value)=>{
    var token  = value.toString();
    console.log("TOKEN IN REMEDYSERVICES="+token);
    headers.append('Authorization', token);
    console.log("header value inside="+JSON.stringify(headers));
    console.log("header value outside="+JSON.stringify(headers));
   let options = new RequestOptions({ headers: headers });
   this.response = this.http.post(url,this.urlEncode(params),options).map(res => res.json());
   console.log("Response JSON="+JSON.stringify(this.response));
   return this.response.toPromise();
})

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

IonViewDidEnter “Promise” ionic 2

分類Dev

Angular 2: Convert Observable to Promise

分類Dev

Angular 2: Convert Observable to Promise

分類Dev

Returning a promise value in Angular 2, Ionic 2

分類Dev

ionic2 Uncaught(in promise):ReferenceError:cordova is not defined .. when using InAppBrowserplugin

分類Dev

Angular 2 Firebase Observable to promise doesn't return anything

分類Dev

ionic2のpromise関数内のnav

分類Dev

Convert Promise to RxJs Observable

分類Dev

Angular / Ionic Observable / PromiseのAsyncPipeでデフォルト値を返す方法

分類Dev

Ionic 2 using a function from another class

分類Dev

Angular2コールバックとPromise / Observable

分類Dev

Angular 2、Ionic2でpromise値を返す

分類Dev

ionic 2マップの読み込み "Uncaught(in promise):[object PositionError]"

分類Dev

Ionic 2Promiseサーバーとは異なるデータ

分類Dev

kotlin getting a subscriber to observe an observable using RxJava2

分類Dev

Ionic 2 / Type Script / Angular2の再帰関数からPromiseを返す

分類Dev

How to use redux-observable and promise correctly?

分類Dev

Angular - APP_INITIALIZER - Promise vs Observable

分類Dev

Catching errors in Promise, Observable.fromPromise() and subscribtion

分類Dev

How to locate an error in an Angular promise or observable?

分類Dev

Promise内でObservableを返す

分類Dev

One Promise Executed at time in an Rx.Observable

分類Dev

how to return observable from function that has a promise

分類Dev

Ionic 2:作成されたObservableからエラーを取得する方法は?

分類Dev

ionic2アプリケーションで観察可能なpromiseの使用

分類Dev

Ionic2 RestApi Promise は配列でのみ機能しますか?

分類Dev

Ionic Angular Uncaught(in promise)エラー

分類Dev

Angular 2 temlate with Observable

分類Dev

Angularfire 2 and Ionic 2

Related 関連記事

  1. 1

    IonViewDidEnter “Promise” ionic 2

  2. 2

    Angular 2: Convert Observable to Promise

  3. 3

    Angular 2: Convert Observable to Promise

  4. 4

    Returning a promise value in Angular 2, Ionic 2

  5. 5

    ionic2 Uncaught(in promise):ReferenceError:cordova is not defined .. when using InAppBrowserplugin

  6. 6

    Angular 2 Firebase Observable to promise doesn't return anything

  7. 7

    ionic2のpromise関数内のnav

  8. 8

    Convert Promise to RxJs Observable

  9. 9

    Angular / Ionic Observable / PromiseのAsyncPipeでデフォルト値を返す方法

  10. 10

    Ionic 2 using a function from another class

  11. 11

    Angular2コールバックとPromise / Observable

  12. 12

    Angular 2、Ionic2でpromise値を返す

  13. 13

    ionic 2マップの読み込み "Uncaught(in promise):[object PositionError]"

  14. 14

    Ionic 2Promiseサーバーとは異なるデータ

  15. 15

    kotlin getting a subscriber to observe an observable using RxJava2

  16. 16

    Ionic 2 / Type Script / Angular2の再帰関数からPromiseを返す

  17. 17

    How to use redux-observable and promise correctly?

  18. 18

    Angular - APP_INITIALIZER - Promise vs Observable

  19. 19

    Catching errors in Promise, Observable.fromPromise() and subscribtion

  20. 20

    How to locate an error in an Angular promise or observable?

  21. 21

    Promise内でObservableを返す

  22. 22

    One Promise Executed at time in an Rx.Observable

  23. 23

    how to return observable from function that has a promise

  24. 24

    Ionic 2:作成されたObservableからエラーを取得する方法は?

  25. 25

    ionic2アプリケーションで観察可能なpromiseの使用

  26. 26

    Ionic2 RestApi Promise は配列でのみ機能しますか?

  27. 27

    Ionic Angular Uncaught(in promise)エラー

  28. 28

    Angular 2 temlate with Observable

  29. 29

    Angularfire 2 and Ionic 2

ホットタグ

アーカイブ