Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NavGuards with async HTTP Calls does not work v3 and probably also v2 #11090

Closed
mburger81 opened this issue Apr 6, 2017 · 6 comments
Closed

Comments

@mburger81
Copy link
Contributor

Ionic version: (check one with "x")
[ ] 1.x
[x] 2.x
[3] 2.x

I'm submitting a ... (check one with "x")
[x] bug report
[x] feature request
[ ] support request => Please do not submit support requests here, use one of these channels: https://forum.ionicframework.com/ or http://ionicworldwide.herokuapp.com/

Current behavior:
We are trying to handle a NavGuard calling a HTTP request by an Obersvable. We have an get for current/user in an service, something like that:

    current() {

        return this.http.get('/api/v1/users/current')
                        .map(res => res.json());
    }

for our lazy loading page on ionic3 we will check if user is authenticated on ionViewCanEnter, so this has to be async.

We are doing this:

ionViewCanEnter(): Promise<any> {
        return new Promise((resolve, reject) => {

                /*
                this.userService.current().toPromise()
                    .then(data => {console.log('data'); resolve()})
                    .catch(err => {console.log('err'); resolve()})
                */

                this.userService.current()
                        .subscribe(
                            (data) => {
                                console.log('User is: ', data);
                                resolve('OK');
                            },
                            (err) => {
                                console.log('User is NOT AUTHENTICATED: ', err);

                                //Redirect the user before denying them access to this route
                                //this.router.navigate(['/login']);
                                reject('ERR');
                            },
                            () => {
                                console.log('????');
                                reject('???');
                            });

            });
    }

The http call is never started, so it looks like there is now subscription to observable. It's difficult to debug, but I think it could be a problem by combine Observables with promises.
Also the commented code with toPromise() for observable does not work.

Expected behavior:
The thing is, it would be nice if we can call in NavGuards directly some httpCalls, but this for now we can not do. I don't know if this is a bug of Promise/Observable or Ionic.

Other information:

Ionic info: (run ionic info from a terminal/cmd prompt and paste output below):

    Cordova CLI: 6.5.0 
    Ionic Framework Version: 3.0.0
    Ionic CLI Version: 3.0.0-beta.5
    ios-deploy version: Not installed
    ios-sim version: Not installed
    OS: Linux 4.8
    Node Version: v6.10.2
    Xcode version: Not installed
@jgw96 jgw96 added the v3 label Apr 7, 2017
@jgw96
Copy link
Contributor

jgw96 commented Apr 7, 2017

Hello, thanks for using Ionic! We will look into this.

@jgw96 jgw96 closed this as completed Apr 7, 2017
@jgw96 jgw96 reopened this Apr 7, 2017
@zbarbuto
Copy link

@mburger81 What happens if you change it to the following:

ionViewCanEnter(): Promise<any> {
  return this.userService.current().toPromise();
}

@mburger81
Copy link
Contributor Author

@zbarbuto It works but there is also a BUG on it
I'm not sure when this is happening but on doing this I get an error on some situation
This is an example code which alls an Obersvable and get a promise from it, but in some situations I get an error see my attached ISSUE which I opened below

    ionViewCanEnter(): Promise<any> {
        return new Promise((resolve, reject) => {
                this.userService.current(true).toPromise()
                        .then((data) => {
                            resolve();
                        })
                        .catch((error) => {
                            reject('ERROR');
                        });
        });
    }

#11168

@jgw96
Copy link
Contributor

jgw96 commented May 15, 2017

Hello! Thanks for opening an issue with us! As this seems like more of a support question related to using observables, I will urge that you ask this question on our forum (forum.ionicframework.com) or on our [slack channel](https://ionic-worldwide.slack.com/messages?]. Thanks for using Ionic!

@jgw96 jgw96 closed this as completed May 15, 2017
@mburger81
Copy link
Contributor Author

Comm on guys! This is not a support request. This was definitly a bug you closed some release ago with version 3.0.1. We don't know if it was a side effect or something else.

@ionitron-bot
Copy link

ionitron-bot bot commented Sep 3, 2018

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.

@ionitron-bot ionitron-bot bot locked and limited conversation to collaborators Sep 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants