3

I have a for-loop running in vue using v-for that loops through a media object to see if there are images stored. That works correctly, but now I want a div displayed below it saying "There is no media" if the object is empty. The text "There is no media" always displays though. Any help on this would be greatly appreciated.

Below is my code:

<v-flex>
                            <p><v-flex sm3 v-for="media in current_sample.media"  :key="media.id">
                                <v-btn @click="delete_image(media.id)">[X]</v-btn> <img :src="'https://orderinformation.s3.amazonaws.com/' + media.path" class="rotateImg" />
                            </v-flex>
                                <v-flex sm3 v-if="current_sample.media">There is no media</v-flex>
                            </p>
                        </v-flex>
2
  • Test specific property instead of object, like v-if="current_sample.media.path !== '' " Commented Oct 2, 2019 at 15:21
  • @Deda I did it now says this error: "Cannot read property 'path' of undefined" in console Commented Oct 2, 2019 at 15:31

2 Answers 2

4

You can use Object.entries property :

 <v-flex sm3 v-if="!Object.entries(current_sample.media).length">There is no media</v-flex>
Sign up to request clarification or add additional context in comments.

2 Comments

That solution only works in browsers which supports ECMA 7+ See my solution which works in all browsers
@protestator That doesnt work it says "Cannot convert undefined or null to object" now <v-flex sm3 v-for="media in current_sample.media" :key="media.id"> <v-btn @click="delete_image(media.id)">[X] </v-btn> <img :src="'orderinformation.s3.amazonaws.com' + media.path" class="rotateImg PreviewImage" /> </v-flex> <v-flex sm3 v-if="!Object.entries(current_sample.media).length">There is no media</v-flex>
2

You can use Object.keys which is compatible with all browsers

v-if="Object.keys(obj).length"

2 Comments

It now says "Cannot convert undefined or null to object" <p> <v-flex sm3 v-for="media in current_sample.media" :key="media.id" style="float:left;"> <v-btn @click="delete_image(media.id)">[X] </v-btn> <img :src="'orderinformation.s3.amazonaws.com' + media.path" class="rotateImg" style="float:left;" /> </v-flex> <v-flex sm3 v-if="Object.keys(current_sample.media).length">There is no media</v-flex> </p>
Try this: v-if="current_sample.media && Object.keys(current_sample.media).length"

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.