6

I am new to Vue.js.

Please advice me.
I get comments: undefined so comments are not displaying. xhr is normal with 200.

Thank you Thank you Thank you Thank you Thank you

<template>
 <div>
  <ul class="media-list">
     <li class="media" v-for="comment in comments">
         {{ $comment.body }}            
     </li>
  </ul> 
</div>
</template>


<script>
 export default {
    data () {
        return {
            comments: []
        }
    },
    props: {
        postid: null
    },
    methods: {
        getComments () {
            this.$http.get('/blog/' + this.postid + '/comments').then((response) => {
                this.comments = response.json().data;
            });
        }
    },
    mounted () {
        this.getComments();
    }
}
2
  • 1
    {{ comment.body }} instead of {{ $comment.body }} Commented May 28, 2017 at 15:48
  • 1
    bind(this) is missing - this.$http.get('/blog/' + this.postid + '/comments').then((response) => { this.comments = response.json().data; }.bind(this)); Commented May 29, 2017 at 5:21

4 Answers 4

3

Basically there are two problems:

  1. $comment don't exist
  2. You have no data on response.json().data, that's why you get a undefined

I used a different API just to test it (as I don't have access to yours).

TEMPLATE

<div id="app">
  <ul class="media-list">
    <li class="media" v-for="comment in comments">
      {{ comment.familyName + ', ' + comment.givenName }}
    </li>
  </ul>
</div>

SCRIPT

new Vue({
    el: '#app',
  data () {
    return {
      comments: []
    }
  },
  props: {
    postid: null
  },
  methods: {
    getComments () {
      this.$http.get('//ergast.com/api/f1/drivers.json').then((response) => {
        this.comments = response.body.MRData.DriverTable.Drivers;
      });
    }
  },
  mounted () {
    this.getComments();
  }
});

Check out a working example here

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

Comments

0
this.comments = response.json().data;

console.log(this.comments) ;

to see what you get ; you define comments=Array ; maybe you get the response.json().data is not a Array;

Comments

0

Try using vm instead of this. In API response make sure what you are getting using console.log(). If response is already in json do not use response.json(). In HTML change $comment.body to comment.body. Make sure you have the body key in comments[] array.

<template>
<div>
  <ul class="media-list">
   <li class="media" v-for="comment in comments">
      {{ comment.body }}            
   </li>
  </ul> 
</div>
</template>

<script>
  export default {
    data () {
      return {
        comments: [],
        postid: null
      }
    },
    props: {

    },
    methods: {
      getComments () {
        let vm = this;
        vm.$http.get('/blog/' + vm.postid + 
            '/comments').then((response) => {

            console.log(response)
            vm.comments = response.data;
        });
      }
    },
    mounted () {
      let vm = this;
      vm.getComments();
    }
  }
}

Comments

0

:

My suggestion is to properly use try-catch statements.

I have found this is the safest and proper way to manage cases where variable could take undefined or null values, instead of trying to "if" everything.

try {
        val = localStorage.getItem('accesstoken')
      } catch (error) {
        alert(error)
      }

Take care!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.