[Solved] Get the id of an element in an element list

I am a student and I am working on a personal project on Meteor.

I have an item list retrieved from my MongoDB database in my template.

I would like to be able to retrieve the ID of each element from a function in a Meteor.call to assign an action related to a button.

Template

<template name="correction_accepted_list">
	{{#each correction in corrections}}
	
		{{> correction_single correction=correction}}
	
	{{/each}}
</template>

<template name="correction_single">
	<div class="card my-2">
		<div class="card-body">
			{{correction.status}}
			<h5 class="card-title">{{getUserFullname correction.ownerId}}</h5>
			<p class="card-text">{{correction.content}} {{correction._id}}</p>
			<button class="js-accept-correction">Accepter</button>
			<button class="js-refuse-correction">Refuser</button>
		</div>
	</div>
</template>

Fonction

	'click .js-accept-correction'(event, instance){
		
		Meteor.call('acceptCorrection', Corrections.find()._id,
				   (err, res) => {
			if(!err) console.log('accepted!')
		})
	},

You can see the result on: http://52.47.162.25/

I use findOne () on the deployed version to have a small result but not the desired one.

I understand my mistake because I do not give any instructions to get the id of the targeted element, but I do not know how to do it for that.

Understanding how to do it would help me for many other things.

Its been awhile since I’ve used blaze, but I think this should be pretty easily solved by supplying the correction as the context for the template inclusion tag.

<template name="correction_accepted_list">
	{{#each correction in corrections}}
		{{> correction_single correction}}
	{{/each}}
</template>

<template name="correction_single">
	<div class="card my-2">
		<div class="card-body">
			{{this.status}}
			<h5 class="card-title">{{getUserFullname this.ownerId}}</h5>
			<p class="card-text">{{this.content}} {{this._id}}</p>
			<button class="js-accept-correction">Accepter</button>
			<button class="js-refuse-correction">Refuser</button>
		</div>
	</div>
</template>
'click .js-accept-correction'(event, instance){
		Meteor.call('acceptCorrection', this._id,
				   (err, res) => {
			if(!err) console.log('accepted!')
		})
	},
1 Like

Thank you.

It works very well and it allows me to better understand.

1 Like