Ionic 3: Ionic Storage with ObservablesHandling refresh tokens using rxjsWhat is the difference between Promises and Observables?BehaviorSubject vs Observable?Generic build request method response undefinedIonic Storage service with observablesRead object field from observable using async pipeAngular/Ionic passing API key in headerHTTP GET cannot read property 'length' when passing a header into the request in ionic 3Combine 3 streams with Observables in series and in parallelCannot display the name parameter of product in dropdown select

How to remove the first colon ':' from a timestamp?

What is the meaning of [[:space:]] in bash?

A Table Representing the altar

How possible is a successful landing just with 1 wing?

Kepler space telescope undetected planets

Is there an English equivalent for "Les carottes sont cuites", while keeping the vegetable reference?

Generating a PIN from cryptographic bytes

Random piece of plastic

Cauchy reals and Dedekind reals satisfy "the same mathematical theorems"

Does the Intel 8085 CPU use real memory addresses?

what relax command means?

Why is Katakana not pronounced Katagana?

Why doesn't philosophy have higher standards for its arguments?

Wordplay subtraction paradox

My credit card has no magnetic stripe. Is this a problem in the USA?

What are the basics of commands in Minecraft Java Edition?

How can a drink contain 1.8 kcal energy while 0 g fat/carbs/protein?

Is this artwork (used in a video game) real?

What would be the safest way to drop thousands of small, hard objects from a typical, high wing, GA airplane?

Is it legal for a supermarket to refuse to sell an adult beer if an adult with them doesn’t have their ID?

Is there a typesafe way to get a Database.QueryLocator?

Why is the total probability theorem expressed in this way?

Finding all possible pairs of square numbers in an array

Is this Android phone Android 9.0 or Android 6.0?



Ionic 3: Ionic Storage with Observables


Handling refresh tokens using rxjsWhat is the difference between Promises and Observables?BehaviorSubject vs Observable?Generic build request method response undefinedIonic Storage service with observablesRead object field from observable using async pipeAngular/Ionic passing API key in headerHTTP GET cannot read property 'length' when passing a header into the request in ionic 3Combine 3 streams with Observables in series and in parallelCannot display the name parameter of product in dropdown select






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I have CustomerPage, CustomerService and AuthService.



In need to pass an access_token from AuthService to CustomerService for http-get request.
AuthService and CustomerService are not working.



Please help me!



Thnx.



CustomerPage:



this.customerService.getCustomersDashboard()
.subscribe(....)


CustomerService:



getCustomersDashboard(): Observable<any> {
var url = "......";
let authHeader = this.authservice.getAuthHeaders(); // HttpHeader with access_token

return this.http.get(url, headers: authHeader ).pipe(
map((resp: Response) =>
let response = this.extractData(resp);
return response;
));


AuthService:
is not working !!!



 getAuthHeaders()
const authtoken = Observable.fromPromise(this.storage.get(`access_token`));

return new HttpHeaders( 'authorization': authtoken ); // => **not working**










share|improve this question






















  • Is your storage API promise based? If it is async -- you can't just pass it to the headers. Use switchMap to read value from the storage and switch to the http-get. If it is sync -- then you dont need a fromPromise there.

    – kos
    Mar 26 at 10:02











  • Ionic storage is promise based ! Can you provide pseudo-code with switchMap? Thnx

    – Alexander
    Mar 26 at 14:32


















1















I have CustomerPage, CustomerService and AuthService.



In need to pass an access_token from AuthService to CustomerService for http-get request.
AuthService and CustomerService are not working.



Please help me!



Thnx.



CustomerPage:



this.customerService.getCustomersDashboard()
.subscribe(....)


CustomerService:



getCustomersDashboard(): Observable<any> {
var url = "......";
let authHeader = this.authservice.getAuthHeaders(); // HttpHeader with access_token

return this.http.get(url, headers: authHeader ).pipe(
map((resp: Response) =>
let response = this.extractData(resp);
return response;
));


AuthService:
is not working !!!



 getAuthHeaders()
const authtoken = Observable.fromPromise(this.storage.get(`access_token`));

return new HttpHeaders( 'authorization': authtoken ); // => **not working**










share|improve this question






















  • Is your storage API promise based? If it is async -- you can't just pass it to the headers. Use switchMap to read value from the storage and switch to the http-get. If it is sync -- then you dont need a fromPromise there.

    – kos
    Mar 26 at 10:02











  • Ionic storage is promise based ! Can you provide pseudo-code with switchMap? Thnx

    – Alexander
    Mar 26 at 14:32














1












1








1








I have CustomerPage, CustomerService and AuthService.



In need to pass an access_token from AuthService to CustomerService for http-get request.
AuthService and CustomerService are not working.



Please help me!



Thnx.



CustomerPage:



this.customerService.getCustomersDashboard()
.subscribe(....)


CustomerService:



getCustomersDashboard(): Observable<any> {
var url = "......";
let authHeader = this.authservice.getAuthHeaders(); // HttpHeader with access_token

return this.http.get(url, headers: authHeader ).pipe(
map((resp: Response) =>
let response = this.extractData(resp);
return response;
));


AuthService:
is not working !!!



 getAuthHeaders()
const authtoken = Observable.fromPromise(this.storage.get(`access_token`));

return new HttpHeaders( 'authorization': authtoken ); // => **not working**










share|improve this question














I have CustomerPage, CustomerService and AuthService.



In need to pass an access_token from AuthService to CustomerService for http-get request.
AuthService and CustomerService are not working.



Please help me!



Thnx.



CustomerPage:



this.customerService.getCustomersDashboard()
.subscribe(....)


CustomerService:



getCustomersDashboard(): Observable<any> {
var url = "......";
let authHeader = this.authservice.getAuthHeaders(); // HttpHeader with access_token

return this.http.get(url, headers: authHeader ).pipe(
map((resp: Response) =>
let response = this.extractData(resp);
return response;
));


AuthService:
is not working !!!



 getAuthHeaders()
const authtoken = Observable.fromPromise(this.storage.get(`access_token`));

return new HttpHeaders( 'authorization': authtoken ); // => **not working**







angular ionic-framework rxjs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 26 at 9:10









AlexanderAlexander

82 bronze badges




82 bronze badges












  • Is your storage API promise based? If it is async -- you can't just pass it to the headers. Use switchMap to read value from the storage and switch to the http-get. If it is sync -- then you dont need a fromPromise there.

    – kos
    Mar 26 at 10:02











  • Ionic storage is promise based ! Can you provide pseudo-code with switchMap? Thnx

    – Alexander
    Mar 26 at 14:32


















  • Is your storage API promise based? If it is async -- you can't just pass it to the headers. Use switchMap to read value from the storage and switch to the http-get. If it is sync -- then you dont need a fromPromise there.

    – kos
    Mar 26 at 10:02











  • Ionic storage is promise based ! Can you provide pseudo-code with switchMap? Thnx

    – Alexander
    Mar 26 at 14:32

















Is your storage API promise based? If it is async -- you can't just pass it to the headers. Use switchMap to read value from the storage and switch to the http-get. If it is sync -- then you dont need a fromPromise there.

– kos
Mar 26 at 10:02





Is your storage API promise based? If it is async -- you can't just pass it to the headers. Use switchMap to read value from the storage and switch to the http-get. If it is sync -- then you dont need a fromPromise there.

– kos
Mar 26 at 10:02













Ionic storage is promise based ! Can you provide pseudo-code with switchMap? Thnx

– Alexander
Mar 26 at 14:32






Ionic storage is promise based ! Can you provide pseudo-code with switchMap? Thnx

– Alexander
Mar 26 at 14:32













2 Answers
2






active

oldest

votes


















1














To read async data and then make a request with it — use a switchMap operator



getCustomersDashboard() 
const url = '//...';
// read data from storage
return from(this.storage.get('access_token')).pipe(
// now switch to a http-get request
switchMap(access_token =>
// make a HttpHeaders from it
const headers = new HttpHeaders( 'authorization': access_token );
return this.http.get(url, headers )
),
// here you'll have your request results
map(response =>
this.extractData(response)
)
)



Note that fromPromise is now just from.



Hope this helps






share|improve this answer

























  • Not working, "The 'this' context of type 'void' is not assignable to methods 'this' of type 'Observable<>'

    – Alexander
    Mar 26 at 16:00











  • @Alexander, its hard to understand what this error indicates. What line it is?

    – kos
    Mar 26 at 16:06











  • all lines in switchMap

    – Alexander
    Mar 26 at 16:17











  • @Alexander, hmm, I'm not sure why TS might complain there (apart from my misspelling access_token). Could you create a stackblitz for this? Its was rather a pseudocode.

    – kos
    Mar 26 at 16:28











  • stackblitz.com/edit/ionic-w4tp3x

    – Alexander
    Mar 26 at 16:42


















0














you can use this:



this.local.get('access_token');


or else you can use these methods:



for setting the token:



this.local = new Storage(LocalStorage);
this.local.set("YourTokenValue", this.access_token);


for Getting the token:



toCheckStatus();
function toCheckStatus()

self.local = new Storage(LocalStorage);
self.local.get('access_token').then((token) =>

console.log("token", token);
);






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55353362%2fionic-3-ionic-storage-with-observables%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    To read async data and then make a request with it — use a switchMap operator



    getCustomersDashboard() 
    const url = '//...';
    // read data from storage
    return from(this.storage.get('access_token')).pipe(
    // now switch to a http-get request
    switchMap(access_token =>
    // make a HttpHeaders from it
    const headers = new HttpHeaders( 'authorization': access_token );
    return this.http.get(url, headers )
    ),
    // here you'll have your request results
    map(response =>
    this.extractData(response)
    )
    )



    Note that fromPromise is now just from.



    Hope this helps






    share|improve this answer

























    • Not working, "The 'this' context of type 'void' is not assignable to methods 'this' of type 'Observable<>'

      – Alexander
      Mar 26 at 16:00











    • @Alexander, its hard to understand what this error indicates. What line it is?

      – kos
      Mar 26 at 16:06











    • all lines in switchMap

      – Alexander
      Mar 26 at 16:17











    • @Alexander, hmm, I'm not sure why TS might complain there (apart from my misspelling access_token). Could you create a stackblitz for this? Its was rather a pseudocode.

      – kos
      Mar 26 at 16:28











    • stackblitz.com/edit/ionic-w4tp3x

      – Alexander
      Mar 26 at 16:42















    1














    To read async data and then make a request with it — use a switchMap operator



    getCustomersDashboard() 
    const url = '//...';
    // read data from storage
    return from(this.storage.get('access_token')).pipe(
    // now switch to a http-get request
    switchMap(access_token =>
    // make a HttpHeaders from it
    const headers = new HttpHeaders( 'authorization': access_token );
    return this.http.get(url, headers )
    ),
    // here you'll have your request results
    map(response =>
    this.extractData(response)
    )
    )



    Note that fromPromise is now just from.



    Hope this helps






    share|improve this answer

























    • Not working, "The 'this' context of type 'void' is not assignable to methods 'this' of type 'Observable<>'

      – Alexander
      Mar 26 at 16:00











    • @Alexander, its hard to understand what this error indicates. What line it is?

      – kos
      Mar 26 at 16:06











    • all lines in switchMap

      – Alexander
      Mar 26 at 16:17











    • @Alexander, hmm, I'm not sure why TS might complain there (apart from my misspelling access_token). Could you create a stackblitz for this? Its was rather a pseudocode.

      – kos
      Mar 26 at 16:28











    • stackblitz.com/edit/ionic-w4tp3x

      – Alexander
      Mar 26 at 16:42













    1












    1








    1







    To read async data and then make a request with it — use a switchMap operator



    getCustomersDashboard() 
    const url = '//...';
    // read data from storage
    return from(this.storage.get('access_token')).pipe(
    // now switch to a http-get request
    switchMap(access_token =>
    // make a HttpHeaders from it
    const headers = new HttpHeaders( 'authorization': access_token );
    return this.http.get(url, headers )
    ),
    // here you'll have your request results
    map(response =>
    this.extractData(response)
    )
    )



    Note that fromPromise is now just from.



    Hope this helps






    share|improve this answer















    To read async data and then make a request with it — use a switchMap operator



    getCustomersDashboard() 
    const url = '//...';
    // read data from storage
    return from(this.storage.get('access_token')).pipe(
    // now switch to a http-get request
    switchMap(access_token =>
    // make a HttpHeaders from it
    const headers = new HttpHeaders( 'authorization': access_token );
    return this.http.get(url, headers )
    ),
    // here you'll have your request results
    map(response =>
    this.extractData(response)
    )
    )



    Note that fromPromise is now just from.



    Hope this helps







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited May 2 at 21:01

























    answered Mar 26 at 14:49









    koskos

    2,1111 gold badge5 silver badges17 bronze badges




    2,1111 gold badge5 silver badges17 bronze badges












    • Not working, "The 'this' context of type 'void' is not assignable to methods 'this' of type 'Observable<>'

      – Alexander
      Mar 26 at 16:00











    • @Alexander, its hard to understand what this error indicates. What line it is?

      – kos
      Mar 26 at 16:06











    • all lines in switchMap

      – Alexander
      Mar 26 at 16:17











    • @Alexander, hmm, I'm not sure why TS might complain there (apart from my misspelling access_token). Could you create a stackblitz for this? Its was rather a pseudocode.

      – kos
      Mar 26 at 16:28











    • stackblitz.com/edit/ionic-w4tp3x

      – Alexander
      Mar 26 at 16:42

















    • Not working, "The 'this' context of type 'void' is not assignable to methods 'this' of type 'Observable<>'

      – Alexander
      Mar 26 at 16:00











    • @Alexander, its hard to understand what this error indicates. What line it is?

      – kos
      Mar 26 at 16:06











    • all lines in switchMap

      – Alexander
      Mar 26 at 16:17











    • @Alexander, hmm, I'm not sure why TS might complain there (apart from my misspelling access_token). Could you create a stackblitz for this? Its was rather a pseudocode.

      – kos
      Mar 26 at 16:28











    • stackblitz.com/edit/ionic-w4tp3x

      – Alexander
      Mar 26 at 16:42
















    Not working, "The 'this' context of type 'void' is not assignable to methods 'this' of type 'Observable<>'

    – Alexander
    Mar 26 at 16:00





    Not working, "The 'this' context of type 'void' is not assignable to methods 'this' of type 'Observable<>'

    – Alexander
    Mar 26 at 16:00













    @Alexander, its hard to understand what this error indicates. What line it is?

    – kos
    Mar 26 at 16:06





    @Alexander, its hard to understand what this error indicates. What line it is?

    – kos
    Mar 26 at 16:06













    all lines in switchMap

    – Alexander
    Mar 26 at 16:17





    all lines in switchMap

    – Alexander
    Mar 26 at 16:17













    @Alexander, hmm, I'm not sure why TS might complain there (apart from my misspelling access_token). Could you create a stackblitz for this? Its was rather a pseudocode.

    – kos
    Mar 26 at 16:28





    @Alexander, hmm, I'm not sure why TS might complain there (apart from my misspelling access_token). Could you create a stackblitz for this? Its was rather a pseudocode.

    – kos
    Mar 26 at 16:28













    stackblitz.com/edit/ionic-w4tp3x

    – Alexander
    Mar 26 at 16:42





    stackblitz.com/edit/ionic-w4tp3x

    – Alexander
    Mar 26 at 16:42













    0














    you can use this:



    this.local.get('access_token');


    or else you can use these methods:



    for setting the token:



    this.local = new Storage(LocalStorage);
    this.local.set("YourTokenValue", this.access_token);


    for Getting the token:



    toCheckStatus();
    function toCheckStatus()

    self.local = new Storage(LocalStorage);
    self.local.get('access_token').then((token) =>

    console.log("token", token);
    );






    share|improve this answer



























      0














      you can use this:



      this.local.get('access_token');


      or else you can use these methods:



      for setting the token:



      this.local = new Storage(LocalStorage);
      this.local.set("YourTokenValue", this.access_token);


      for Getting the token:



      toCheckStatus();
      function toCheckStatus()

      self.local = new Storage(LocalStorage);
      self.local.get('access_token').then((token) =>

      console.log("token", token);
      );






      share|improve this answer

























        0












        0








        0







        you can use this:



        this.local.get('access_token');


        or else you can use these methods:



        for setting the token:



        this.local = new Storage(LocalStorage);
        this.local.set("YourTokenValue", this.access_token);


        for Getting the token:



        toCheckStatus();
        function toCheckStatus()

        self.local = new Storage(LocalStorage);
        self.local.get('access_token').then((token) =>

        console.log("token", token);
        );






        share|improve this answer













        you can use this:



        this.local.get('access_token');


        or else you can use these methods:



        for setting the token:



        this.local = new Storage(LocalStorage);
        this.local.set("YourTokenValue", this.access_token);


        for Getting the token:



        toCheckStatus();
        function toCheckStatus()

        self.local = new Storage(LocalStorage);
        self.local.get('access_token').then((token) =>

        console.log("token", token);
        );







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 26 at 9:20









        Pupinder SinghPupinder Singh

        54 bronze badges




        54 bronze badges



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55353362%2fionic-3-ionic-storage-with-observables%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript