Reputation: 1657
I want to set the content of an textarea by getting the data of a nested array. In the helper I define the document id and the element id. Now I want to get the content of text
for displaying in the template.
But my problem is to get the exact field out of the findOne-result, so the spacebar helper {{infotext}}
is obviously wrong:
Document JSON
{
"_id" : "8rmzAN3yw79TAKNeD",
"title" : "title",
"tooltips" : [
{
"element" : "16091688",
"text" : "Lorem"
},
{
"element" : "944a0d46",
"text" : "ipsum"
}
]
}
Helper
Template.infotext.helpers({
infotext: function() {
var id = '8rmzAN3yw79TAKNeD',
elementId = Session.get('elementID');
return Articles.findOne({_id: id, 'tooltips.element': elementId});
}
});
Template
<template name="infotext">
<form>
<textarea>{{infotext}}</textarea>
<input type="submit" class="btn btn-default" value="save">
</form>
</template>
Upvotes: 1
Views: 602
Reputation: 21
have the same issue,thanks, This helper works for me :
userAmount(){
let item =Items.findOne({_id:this._id,'cart.user':Meteor.userId()});
let amount ='';
if(item){
item.cart.filter(function (cart) {
if(cart.user ==Meteor.userId()){
amount =Number(cart.amount);}else{amount=0;}
}
}else{amount=0;}
return amount;
}
Upvotes: 2
Reputation: 515
Your helper is returning the entire document when you are only after information in the document. You definitely need to modify the helper function. (note this code is untested and may not actually work but will give you a bit of info on what to do)
Template.infotext.helpers({
infotext: function() {
var id = '8rmzAN3yw79TAKNeD',
elementId = Session.get('elementID');
var doc = Articles.findOne({_id: id, 'tooltips.element': elementId});
var text = '';
if (doc) {
var filtered = doc.tooltips.filter(function(tooltip) {
if (tooltip.element == Session.get('elementID')) {
return tooltip;
}
return false;
});
if (filtered[0]) {
text = filtered[0].text; //returns the first match - assuming there is no duplicates
}
}
return text;
}
});
Upvotes: 2