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

237
Views
obtenga "_id" de [ ] en v-for loop y reutilícelo en data // vuecli

Estoy haciendo un bucle en [postArray] para obtener algunas publicaciones, cada publicación tiene su propio "_id". Y quiero reutilizar este "_id" para agregar Me gusta a la publicación correcta.

este es el bucle v-for:

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

estos son los datos donde quiero almacenar mi ID en postId:

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

},

y estos son los métodos en los que quiero reutilizar el _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); },

también la pantalla de la consola.log(datos):

captura de pantalla de console.log

[ingrese la descripción de la imagen aquí][2]

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

0

Intenta enviar _id a tu método:

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

y luego recibirlo:

 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!