1

I have a place for displaying user info, when page refreshed i read user info from an ajax call and set user info data.but because of Time-consuming process of axios call Data setup is performed after the DOM rendering.

HTML :

<div> {{user.name}} </div>

vue.js :

data: function(){
 return {
   user: {},
   }
},

methods: { 

getUserInfo() {
   axios.post(laroute.route('link'), data).then((response) => {
      this.$set(this, 'user', response.data.user);
        },
 },

 mounted() {

        this.getUserInfo();
    }

after refreshing page i got this error:

"TypeError: Cannot read property 'name' of undefined"
4
  • getUserInfo() should under methods: { getUserInfo() { ... } } Commented Jun 25, 2018 at 10:31
  • The code you posted is incomplete, cannot help you find the reason. and you should check response.data.user, maybe is undefined Commented Jun 25, 2018 at 10:35
  • What is data property in axios.post(laroute.route('link'), data)? Can your please post stackoverflow.com/help/mcve. Commented Jun 25, 2018 at 11:05
  • @joaner : Both data sent from axios and response data is correct Commented Jun 25, 2018 at 11:23

1 Answer 1

1

I cant add a comment. You can try to change:

mounted for created and this.$set(this, 'user', response.data.user); for user = response.data.user

Anyway, can you console.log the result of call?

Sign up to request clarification or add additional context in comments.

Comments

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.