[Vuejs]-Scoping for HTTP request

1đź‘Ť

âś…

The post is an async request, meaning that when you log this.pass_or_fail, the function passed to the then method hasn’t fired yet.

You’ll need to put any code depending on the result of the post request inside of the function being passed to then():

this.$http.post('yoururl')
  .then((response) => {
    if (response.body == "success") {
      this.pass_or_fail = response.body;

      console.log(this.pass_or_fail);

      this.$router.push({ name: 'landing-page' });
    }
  });
👤thanksd

1đź‘Ť

Because you are attempting to synchronously get the value of this.pass_or_fail while it’s result is still obtained. I imagine it returns undefined. You need to “wait” for the response, before you can use it. The simplest way is to handle your logic inside the then callback. If you don’t want to do that, you can use Promises or Async/Await

👤ilrein

Leave a comment