0

In a blog-like Vue.js app I'd like to update list of comments of a post with new comments, when a new comment is successfully posted to the server.

Here is my code:

<div v-for="(post, index) in posts" :key="index" >  
    <div>{{post.body}}</div>
    <div class="comments">
        <ul v-for="(c, j) in post.comments" :key="j" class="comment">                      
          <li>{{c.user_id}} : {{c.body}} </li>
        </ul>    
    </div>

     <div>                  
        <input type="text" v-on:keyup.enter="submitComment(post)" v-model="post.comment" placeholder=" Add your comment" />                  
     </div>
</div>

And the method:

  submitComment(post) {
    const formData = new FormData();
    formData.append('token', this.token);
    formData.append('pid', post.id);
    formData.append('body', post.comment);
    axios.post(this.BASE_URL + "/api/comment", formData)
    .then( (res)=> {
      console.log('Updated comments are', res.data.comments);
      this.$set(post, 'comment', res.data.comments) ; //Here is the problem
    })
    .catch( error => {  
      console.log(error);
    });
  }

Despite the fact that I receive the updated list of comments, empty results is rendered. How can I fix this?

3
  • 1
    Can you please post the json response you get from the server? Commented Jan 16, 2019 at 10:21
  • 2
    should it be this.$set(post, 'comments', res.data.comments) with comments in plural form? Commented Jan 16, 2019 at 10:22
  • @VladNeacsu You are right. It was my silly mistake. Please answer and I'll accept. Commented Jan 16, 2019 at 10:22

1 Answer 1

1

as @blaz pointed out in the comments above, it looks like the error is from a typo in the $set method call as the property should be the plural comments not comment

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

Comments

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.