Skip to content

Instantly share code, notes, and snippets.

@joeczar
Created July 25, 2020 16:55
Show Gist options
  • Select an option

  • Save joeczar/12517c09cefdbe8a157bacda52b70010 to your computer and use it in GitHub Desktop.

Select an option

Save joeczar/12517c09cefdbe8a157bacda52b70010 to your computer and use it in GitHub Desktop.
Vue component v-for error: [Vue warn]: Property or method "each" is not defined on the instance but referenced during render.
Vue.component('comments', {
props: ['img'],
data: function () {
return {
comment: '',
username: '',
comments: [],
};
},
mounted: function () {
const self = this;
console.log('Sending:', { imageid: this.img });
axios
.post('/comments', { imageid: self.img })
.then(({ data }) => {
this.comments = data;
})
.catch((err) => {
console.log('error in get comments', err);
});
},
methods: {
submitComment: function (e) {
e.preventDefault();
console.log('comment this.img', this.img);
axios
.post('/comment', {
comment: this.comment,
usrName: this.username,
imgId: this.img,
})
.then(({ data }) => {
console.log(data);
});
},
},
template: `
<div class="commentWrapper">
<h1 id="commentTitle">Comments</h1>
<form id="commentForm" class="commentForm shadow-3">
<label for="comment">
<textarea v-model="comment" name="comment" id="comment" placeholder="Type your comment here." class="shadow-2"></textarea>
</label>
<label for="username">
<input v-model="username" id="username" placeholder="Username" class="shadow-2">
</label>
<button @click=submitComment class="shadow-3 btn" id="commentSubmit">Submit</button>
</form>
<hr>
<div class="commentsArrayWrapper>
<div v-for='each in comments'>
<p class="commentBody">{{each.comment}}</p>
<span class="userName">From: {{each.username}}</span>
<span class="date">On: {{each.date}}</span>
</div>
</div>
</div>
`,
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment