1

I'm stuck on a strange error: when trying to display a state variable, vuejs warns me: Error in render function: "TypeError: Cannot read property 'token' of undefined"

My state looks like this (in the vuejs extension):

room:Object
    details:Object
        game_id:"1"
        id:914527404
        max_users:5
        messages:Array[0]
        token:"ef6464692f4cce187fe129d7"
        user:Array[1]
    messages:Array[0]
    users:Array[1]

Despite the error, the HTML is still rendered correctly:

<button id="copySharingLink" type="button" class="btn btn-primary waves-effect waves-light" v-bind:data-token="room.details.token">some text</button>

becomes

<button id="copySharingLink" type="button" data-token="ef6464692f4cce187fe129d7" class="btn btn-primary waves-effect waves-light">some text</button>

I have no idea how to rid out of this error, since it's working.

1 Answer 1

1

There may be a time when room or room.details is not yet set, and that may be when the error is getting thrown.

Try changing the code from this...

v-bind:data-token="room.details.token"

...to this:

v-bind:data-token="room.hasOwnProperty('details') ? room.details.token : ''"
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.