0

My v-for loop looks like this:

<ul v-if="item.categorie_string">
    <li v-for="(cat_int,index) in item.categorie_string.split(',')" :key="index">
        {{myarray[cat_int].categoryname}}
    </li>
</ul>

Variable categorie_string can be a string like ='1,2,3', ='5' or = NULL

To prevent the error where categorie_string == null I added v-if="item.categorie_string" to the parent element <ul> but I still get:

Cannot read properties of undefined (reading 'categoryname')"

Shouldn't this be working? It works fine if I test it with an integer instead of cat_int.

3
  • 3
    How does your "myarray" look like, also is it populated? Commented Sep 1, 2022 at 10:34
  • 1
    If myarray has three elements, then categorie_string should be "0,1,2" and "1,2,3" will give undefined on the last iteration. Commented Sep 1, 2022 at 11:04
  • Good point @Dejan.S, yes 'myarray' is populated by default. The issue was really just cat_int being empty. I could solve it with Laurens solution. Commented Sep 1, 2022 at 11:30

1 Answer 1

1

You should also check if myarray[cat_int] exists:

<ul v-if="item.categorie_string">
    <li v-for="(cat_int,index) in item.categorie_string.split(',')" :key="index">
        <span v-if="myarray[cat_int]">{{myarray[cat_int].categoryname}}</span>
    </li>
</ul>
Sign up to request clarification or add additional context in comments.

1 Comment

You don't need a span just for a v-if, you can use <template v-if=""></template>

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.