0

Had to resubmit this question as someone incorrectly closed it. Anyhow I am stuck on an issue with the view not updating when I update an array.

I have the following html

<div class="wall" id="wall">
      <div v-for="item in tweet_data" :key="item.id" class="tweet-box">
          // More code here
      </div>
</div>

and my vue code looks like the following

var main_twitter_feed = new Vue({
  el: '#wall',
  data: {
    tweet_data: []
  },
  methods: {
    fetch_latest_twitter_feed: function() {
      fetch('/api/fetch-main-twitter-feed').then(function(response) {
        if (response.status !== 200) {
          console.log('Looks like there was a problem. Status Code: ' + response.status);
          return;
        }
        // Examine the text in the response
        response.json().then(function(data) {
          if (data.hasOwnProperty('data')) {
            this.tweet_data = data.data
            console.log(this.tweet_data);
          }
        });
      }).catch(function(err) {
        console.log('Fetch Error :-S', err);
      });
      //    
    }
  }
})

When I call the method the "tweet_data" gets updated but this does not update the v-for inside the view. However before I moved the fetch_latest_twitter_feed() inside the vue methods I was calling it outside more like a traditional function

function fetch_latest_twitter_feed() {
  fetch('/api/fetch-main-twitter-feed').then(function(response) {
    if (response.status !== 200) {
      console.log('Looks like there was a problem. Status Code: ' + response.status);
      return;
    }
    // Examine the text in the response
    response.json().then(function(data) {
      console.log(data);
      if (data.hasOwnProperty('data')) {
        main_twitter_feed.tweet_data = data.data
      }
    });
  }).catch(function(err) {
    console.log('Fetch Error :-S', err);
  });
}

and the v-for updated without a problem. Can someone please help and explain the difference. I can't see why moving the function inside of the methods section would alter how the v-for reacts. I call it using the following

window.addEventListener('load', function() {
  main_twitter_feed.fetch_latest_twitter_feed();
  //const interval = setInterval(function() {
  //   fetch_latest_twitter_feed();
  //}, 2000);
})

Edit

I have now used

created() { this.fetch_latest_twitter_feed(); }

and removed the addEventListener but it still does not update the v-for

Thanks

ORStudios
  • 3,157
  • 9
  • 41
  • 69
  • 1
    I already answered this with the same duplicate the [last time you asked it](https://stackoverflow.com/questions/67312407/v-for-not-updating-or-being-called-when-i-set-array-from-within-a-method-in-vue). Where you have `.then(function(something)`, replace it with `.then(something =>`. I did not _"incorrectly"_ close it. Please don't re-post the same question. If you feel it was incorrectly closed, update the question or leave a comment explaining why you think so – Phil Apr 29 '21 at 07:23
  • Sorry my mistake! Thanks for the help – ORStudios Apr 29 '21 at 07:30

0 Answers0