Doubts on the applied use of async/await with Angular

Asked

Viewed 76 times

0

Good morning,

Even reading about the concepts and uses of async and of await I’m still having problems with their actual application.

Basically in my ngOnInit call for a function:

ngOnInit() {
    this.authenticateUser();
  }

Function this:

authenticateUser() {

    console.log("----Autenticando Usuário----");

    this.token = localStorage.getItem("token");
    this.userName = localStorage.getItem("userName");
    this.userPhoto = localStorage.getItem("userPhoto");

    this.currentUser = this.auth.getSession(this.token);

    this.attributions = this.currentUser.sessao.grupos;
    this.userEmail = this.currentUser.sessao.email;
    this.instalation = this.currentUser.instalacao;

   }

The problem is that the currentUser gets the value as null in its execution, because its value is set without receiving the correct value of this.auth.getSession(this.token);

This Auth being built from a service RestApiService

constructor(private auth: RestApiService) { }

Having within this service the method getSession() that returns an object JSON containing user information returned by API.

getSession(xtrToken) {
    xtrToken = "{\"token\":\"" + xtrToken.toString() + "\"}";
    this.http.post(this.apiURL + "/auth", xtrToken)
      .subscribe(function (resposta) {
        if (resposta != null) {
          localStorage.setItem("currentUser", JSON.stringify(resposta));
          if (window.location.href.indexOf("?") > -1) {
            var url = window.location.href;
            var value = url = url.slice(0, url.indexOf('?'));
            value = value.replace('@System.Web.Configuration.WebConfigurationManager.AppSettings["BaseURL"]', '');
            var newUrl = value;
            window.history.pushState({}, null, newUrl);
          }
          this.currentUser = JSON.parse(localStorage.getItem("currentUser"));
        }
      });
      return this.currentUser;
  }

I tried to put in that role getSession as async, and on her call something like this:

async authenticateUser() {

    console.log("----Autenticando Usuário----");

    this.token = localStorage.getItem("token");
    this.userName = localStorage.getItem("userName");
    this.userPhoto = localStorage.getItem("userPhoto");

    this.currentUser = await this.auth.getSession(this.token);

    this.attributions = this.currentUser.sessao.grupos;
    this.userEmail = this.currentUser.sessao.email;
    this.instalation = this.currentUser.instalacao;

   }

But it didn’t do any good. Basically what would be the right way to expect the return of API before "set" the value of this.currentUser ?

  • Mathues, check your getSession API, because it returns "this.currentUser", and http.post is asynchronous, so even before you set the value when the POST receives the response, your API has already returned a null value. The rest of your implementation seems to be correct regarding the use of async/await.

  • the API response is correct, so much so that localStorage.setItem("currentUser", JSON.stringify(reply));

  • Why not save on a Let? This is the owner of the object.

  • this.token returns a correct value, since I pass it as parameter to the api, and it arrow in the localstorage the currentUser correctly. Edit: Save what on Let?

  • Matheus could explain better what he wants to do, where the function is getSession(xtrToken) and where the variable would be auth?

  • I will edit with this information

  • Have you tried asynchronous getSession? Because it’s no use to authenticatUser to call getSession asynchronous when it’s not.

  • yes, I put it but it didn’t help

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.