Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

214
Views
get "_id" from [ ] in v-for loop and reuse it in data // vuecli

i'm looping in [postArray] to get some posts, each post have it's own "_id". And I want to reuse this "_id" to add likes to the correct post.

this is the v-for loop:

<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>

this is the data were i want to store my the id in postId:

data() {
return {
  title: "",
  content: "",
  postArray: [],
  postId: "",
};

},

and this is the methods were i want 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);
},

also the screen of the console.log(data):

console.log screenshot

[enter image description here][2]

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Try to send _id to your method:

<button @click="addLike(element._id)">Add Like</button>

and then receive it:

async addLike(id) {
  ...
  postId: id,
  ...
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!