[英]MeteorJS get MongoDB ID in click event
I have the following mongo collection in my MeteorJS app: 我的MeteorJS应用程序中有以下mongo集合:
//Server side, load all the images
Images = new Mongo.Collection('images');
Meteor.startup(() => {
if(Images.find().count() == 0){
for(var i = 1; i < 23; i++){
Images.insert({
src:"img_"+i+".jpg",
alt:"image "+i
});
}
}
});
I pass that to a template and that works. 我将其传递给模板,并且可以正常工作。 However, I then want to retrieve the MongoDB id
of an image (id that is the primary key/unique ID in MongoDB). 但是,我然后要检索图像的MongoDB id
(该ID是MongoDB中的主键/唯一ID)。 I do it with the following code: 我使用以下代码进行操作:
//Client side, get the collection and load it to the template
Images = new Mongo.Collection('images');
Template.images.helpers({images:Images.find()});
Template.images.events({
'click .js-del-image': (event) => {
var imageId = event._id;
console.log(imageId);
}
});
This gives me undefined
. 这使我undefined
。 What am I doing wrong? 我究竟做错了什么? I thought this._id
should give me the MongoDB ID. 我以为this._id
应该给我MongoDB ID。
For the record, This is my template, the _id
attribute gets filled out: 作为记录,这是我的模板, _id
属性已填写:
<template name="images">
<div class="row">
{{#each images}}
<div class="col-xs-12 col-md-3" id="{{_id}}">
<div class="thumbnail">
<img class="js-image img-responsive thumbnail-img" src="{{src}}"
alt="{{alt}}" />
<div class="caption">
<p>{{alt}}</p>
<button class="js-del-image btn btn-warning">delete</button>
</div>
</div>
</div> <!-- / col -->
{{/each}}
</div> <!-- / row -->
</template>
The problem was in the declaration of a function: 问题出在函数的声明中:
(event) => { ... }
seems to have _id
of undefined
. (event) => { ... }
似乎具有_id
的undefined
。 function(event) {...}
seems to have the correct context. function(event) {...}
似乎具有正确的上下文。 See this answer for further information about (event) => {...}
vs function(){...}
declarations. 有关(event) => {...}
vs function(){...}
声明的更多信息,请参见此答案 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.