Currently, I am iterating through [postArray] to retrieve various posts, each of which has its own unique "_id". My goal is to utilize this "_id" to add likes to the corresponding post.
This is the v-for loop I am using:
<div class="posts" v-for="(element, index) in postArray" :key="index">
<p>{{ element.title }}</p>
<p>{{ element.content }}</p>
<p>{{ element.likes.length }}</p>
<button @click="addLike">Add Like</button>
</div>
Here is where I intend to store the id in postId within my data:
data() {
return {
title: "",
content: "",
postArray: [],
postId: "",
};
In addition, these are the methods I have defined to reuse the _id:
async addLike() {
const url =
"https://dw-s3-nice-master-cake.osc-fr1.scalingo.io/post/like";
const options = {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "bearer " + localStorage.getItem("token"),
},
body: JSON.stringify({
postId: this.postId,
}),
};
const response = await fetch(url, options);
const data = await response.json();
console.log(data);
this.postId = data.posts._id;
console.log(this.postId);
},
Furthermore, you can view the console log screenshot here.
[enter image description here][2]