Hi guys this is my problem: I’m using #each to display a questionnaire, in a form with radio button, I’d like to collect the radio buttons selected in a Collection…The problem is that I can’t keep the insert function working…
HTML
<template name="AddAnswerData">
<div class="well">
<form>
{{#each questions}}
<p><legend>{{qtitle}}</legend></p>
<p>{{qtext}}</p>
{{#each options}}
<p><input type="radio" name="{{this.optqgroup}}" required> {{this.optxt}}</p>
{{/each}}
<br>
{{/each}}
<input type="submit" value="Submit your answers">
</form>
</div>
</template>
JS1
Template.AddAnswerData.helpers({
questions : [
{
qtitle: "1. Nedstämdhet",
qtext: "Avser uppgift",
options:[
{ optqgroup: "q1", optvalue: "q1_opt0", optxt: "0. Neutralt stämningsläge." },
{ optqgroup: "q1", optvalue: "q1_opt1", optxt: "1. " },
{ optqgroup: "q1", optvalue: "q1_opt2", optxt: "2. Övervägande upplevelser" },
{ optqgroup: "q1", optvalue: "q1_opt3", optxt: "3. " },
{ optqgroup: "q1", optvalue: "q1_opt4", optxt: "4. Genomgående nedstämdhet " },
{ optqgroup: "q1", optvalue: "q1_opt5", optxt: "5. " },
{ optqgroup: "q1", optvalue: "q1_opt6", optxt: "6. Genomgående upplevelser " }
]},
{
qtitle: "2. Sänkt grundstämning",
qtext: "Avser en sänkning av ",
options:[
{ optqgroup: "q2", optvalue: "q2_opt0", optxt: "0. Neutral stämningsläge." },
{ optqgroup: "q2", optvalue: "q2_opt1", optxt: "1. " },
{ optqgroup: "q2", optvalue: "q2_opt2", optxt: "2. Ser genomgående " },
{ optqgroup: "q2", optvalue: "q2_opt3", optxt: "3. " },
{ optqgroup: "q2", optvalue: "q2_opt4", optxt: "4. Ser nedstämd " },
{ optqgroup: "q2", optvalue: "q2_opt5", optxt: "5. " },
{ optqgroup: "q2", optvalue: "q2_opt6", optxt: "6. Genomgående " }
]
},
]
});
JS2
Template.AddAnswerData.events({
'submit form': function(event){
event.preventDefault();
var answSel = event.questions.options.optvalue;
AnswerData.insert({
selected: answSel,
});
}
});
Collection
AnswerData.attachSchema(new SimpleSchema({
selected: {
label: "Options Selected",
type: String,
}
}));
I think my problem is in the js1 file when I pass the “event.questions.options.optvalue;”
The console says: “Uncaught TypeError: Cannot read property ‘options’ of undefined”
Can you help me?