5

PLEASE HELP ME!!!!!

When my application starts i run an ajax to get all my feeds. These are objects that I then store in my vue variable which is an array.

    props:['id'],
    data(){
        return {
            feedData: []
        } 
    },
    created(){
        axios.get('/feeds/'+this.id).then(response =>{
            this.feedData = response.data.data;
        });
    }

After that when the user types and sends a new post i store the new post in the database and then emit an event to capture the new post from the database. The function for this is in my methods.

    methods: {
        captureFeed: function (feedId) {
            const vm = this;
            axios.get('/feeds/'+this.id+'/'+feedId).then(response =>{
                vm.feedData.unshift(response.data);
                console.log(response.data);
            });
        }
    },

The weird thing is that when i successfully get the new feed i just created and try to add it to the array of feeds using unshift for some reason the very first post of the array is duplicated and i never get to see the new post until i refresh the page. When i check the console log, i can see that i got the new feed. The funny thing is that when i use

vm.feedData.push(response.data);

it works just fine, but the post is at the very bottom which is not what i want!

i have images to show:

first post:

picture of the first post

second post:

picture of the second post

8
  • What does the template look like? Commented Sep 20, 2017 at 20:49
  • <template> <div class = "col-xs-12 col-md-5 mt-m pr-sm pl-sm"> <timeline-headline></timeline-headline> <div class="panel panel-default"> <div class="panel-body"> <feed-constructor :timeline-id = "id" @newFeedSent = "captureFeed"></feed-constructor> <feed v-for = "feed in feedData" :feedContent="feed"></feed> </div> </div> </div> </template> Commented Sep 20, 2017 at 20:50
  • Try using a key in on the feed that you are looping over. :key="feed.id" where feed.id is some unique value for each feed. Commented Sep 20, 2017 at 20:54
  • let me try that now. will get back to you in 2mins Commented Sep 20, 2017 at 20:59
  • hmm.. i did something like this where i used the feed id. <feed v-for = "feed in feedData" :id="'feed-'+feed.id" :feedContent="feed"></feed> not sure how to use the dynamic key or dynamic feed id to fix this, trying to figure it out Commented Sep 20, 2017 at 21:07

2 Answers 2

2

The main issue here is that when you are looping over a component, you are required to use a key.

In 2.2.0+, when using v-for with a component, a key is now required.

That being the case, your template should look like this:

<feed v-for="feed in feedData" :key="feed.id" :feedContent="feed"></feed> 

where feed.id is a unique value in each feed object. This is because of Vue's DOM update strategy. Using a key helps Vue identify which components need to be created/destroyed.

Sign up to request clarification or add additional context in comments.

Comments

1

Are you using the key param in your template? Like this?

 :key="<An ID>"

For instance:

<div  v-for="(item, index) in array" :key="item.id">
  {{index}} - {{item.name}}
</div>

1 Comment

Thank you @Lucas, your right the :key="<An ID>" helps solve this issue, had no idea, going to research and learn more on this

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.