I'm having quite a hard time with VueJS. In one of my components, I retrieved data from the backend in the form of an array of objects. Once I retrieve this data, I loop through it using forEach
and add a field in each object.
<template>
...
<div v-if="!waitingForComments && comments.length > 0">
<div class="comment" v-for="comment in comments" :key="comment.id">
<p>{{ comment.content }}</p>
<span>Par <router-link :to="{ name: 'user', params: { id: comment.author_id } }">-- {{ comment.author }}</router-link></span>
</div>
</div>
...
</template>
<script>
import { getPost } from "../services/post";
import { getComments } from "../services/comment";
import { getUser } from "../services/user";
export default {
name: 'Post',
data() {
return {
post: null,
comments: [],
postId: null,
waitingForComments: true
}
},
async created() {
this.postId = this.$route.params.id;
this.post = await getPost(this.$route.params.id);
let allComments = await getComments(this.postId);
allComments.forEach(async comment => {
let author = await getUser(comment.author_id);
comment.author = author.firstname + " " + author.lastname;
});
this.comments = allComments;
this.waitingForComments = false;
}
}
</script>
In the <router-link>
, "--" shows with no problem, but comment.author
doesn't print anything ! Yet Vue's Chrome debugger shows that the author fields do hold values !
I really don't understand where this comes from. I tried using await
in front of forEach()
, I also tried using a watcher on comments
and retrieve the authors inside that watcher. But nothing works.
Any help would be much appreciated !