Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

246
Views
Vue how to solve `TypeError: Cannot read properties of undefined` when pushing to array

In my vue-app I try to loop through some video-items, get their youtube-id and fetch their thumbnails.

so I have:

<div class="videos">
  <div v-for="(video, index) in videos" :key="index" :youtube-id="video.id" class="video-item">
    <div @click="play(video.id)">
     <img :src="thumbnail" :width="420" :height="240" />
    </div>
  </div>
</div>

then I have

data(){
  return {
    thumbnail : '',
    thumbnails: [] // don't know if I can use that
   }
},

methods: {
   getVideoThumbNails(){
      const videos = document.querySelector(".videos");
      const iframes = videos.querySelectorAll(".video-item");

      iframes.forEach(function (item, index) {
        let youtube_video_id = item.getAttribute("youtube-id");

        if (youtube_video_id.length === 11) {
           const video_thumbnail = "http://img.youtube.com/vi/" + youtube_video_id + "/maxresdefault.jpg";
        }
        
        // this.thumbnail = video_thumbail
        this.thumbnails.push(video_thumbnail);
     });
   }
},
mounted(){
 this.getVideoThumbNails() 
}

For some reason I don't know, this gives me the error TypeError: Cannot read properties of undefined (reading 'thumbnails')

Can someone tell me what I'm doing wrong? And how can I apply those images in the end?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

First you should use an arrow function as callback for the forEach method in order to get access to the component instance, second you must put the this.thumbnails.push(video_thumbnail); inside the condition block :

      iframes.forEach( (item, index) =>{
        let youtube_video_id = item.getAttribute("youtube-id");

        if (youtube_video_id.length === 11) {
           const video_thumbnail = "http://img.youtube.com/vi/" + youtube_video_id + "/maxresdefault.jpg";

     // this.thumbnail = video_thumbail
        this.thumbnails.push(video_thumbnail);
        }
        
       
     });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!