0

I wonder if someone can explain why adding an axios post request will not update a data value.

After a successful external oauth2 authentication, the code gets the user object and updates this.authd to true. authd is used in a v-if condition to display a block of html, including a logout button. It works. And using vue devtools in firefox, the component data shows what I expect.

Clicking this button then calls a method which posts to /logout and sets this.authd to false if the post works. But the html displayed does not change because authd is still true. Which vue devtools show.

So I added another button that does not call /logout, it just sets this.authd to false. And that does change the data (according to vue devtools) and the html does change.

Here is the html

  <div id="app-3" class="container">
     <template v-if="!authd">
        <a href = "/login">Click here to Github Login</a>
     </template>
     <template v-else>
         Logged in as: <span id="user">{{ user }}</span>
         <div>
             <button v-on:click="logout" class="btn btn-primary">Logout</button>
         </div>
         <div>
           <button v-on:click="otherLogout" class="btn btn-primary"> Special Logout</button>
        </div>
     </template>
  </div>

And the javascript

    vm = new Vue({
        el: '#app-3',
        data: function() {
          return {
            user: null,
            baseurl: "http://mint191:9080/",
            authd: false
          }
        },
        methods: {
          logout: function () {
            axios({
              method: 'post',
              url: 'logout',
              baseURL: this.baseurl
            })
              .then(function(response) {
                  console.log(`post logout: ${JSON.stringify(response)}`)
                  this.user = null
                  this.authd = !this.authd
                  console.log(`logout: authorised is ${this.authd}`)
                  console.log(`logout: user is ${this.user}`)       
                })    
                .catch(function (error) {
                  console.log("logout has gone wrong" + error)
                })   
          },
          otherLogout: function () {
                  this.user = null
                  this.authd = !this.authd
                  console.log(`other logout: authorised is ${this.authd}`)
                  console.log(`other logout: user is ${this.user}`)       
          }
        },
        mounted () {
          axios({
            method: 'get',
            url: 'user',
            baseURL: this.baseurl
          })
            .then(response => {
              console.log(`get user: ${JSON.stringify(response)}`)
              this.user = response.data.userAuthentication.details.login
              this.authd = !this.authd
              console.log(`authorised is ${this.authd}`)
            })
            .catch(function (error) {
              console.log("nothing in user" + error)
            })
        }   
})

The output in the console is what I expected.

So, what am I doing wrong?

1 Answer 1

0

this in logout function is not Vue instance You should use arrow function for that:

 logout: function () {
    axios({
      method: 'post',
      url: 'logout',
      baseURL: this.baseurl
    })
      .then(response => {
          console.log(`post logout: ${JSON.stringify(response)}`)
          this.user = null
          this.authd = !this.authd
          console.log(`logout: authorised is ${this.authd}`)
          console.log(`logout: user is ${this.user}`)       
        })    
        .catch(function (error) {
          console.log("logout has gone wrong" + error)
        })   
  },
Sign up to request clarification or add additional context in comments.

1 Comment

The answer here has a more elaborate answer: [link] (stackoverflow.com/questions/40996344/axios-cant-set-data/…) and an alternative ( .bind(this) ) if do not want to use an arrow function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.