In my mongo collection, I have a document named exam
.
// meteor:PRIMARY> db.exam.find()
{
"_id" : "RLvWTcsrbRXJeTqdB",
"examschoolid" : "5FF2JRddZdtTHuwkx",
"examsubjects" : [
{
"subject" : "Z4eLrwGwqG4pw4HKX"
},
{
"subject" : "fFcWby8ArpboizcT9"
}
],
"examay" : "NrsP4srFGfkc5cJkz",
"examterm" : "5A5dNTgAkdRr5j53j",
"examclass" : "gYF2wE4wBCRy9a3ZC",
"examname" : "First",
"examdate" : ISODate("2016-05-07T22:41:00Z"),
"examresultsstatus" : "notreleased"
}
I am attempting to extract data from this document and store it in another document using the following code. The goal is to use the value of examsubjects
from the previous document as the key in the new document.
'click .reactive-table tr': function() {
Session.set('selectedPersonId', this._id);
var cursor = Exam.find({ _id:
Session.get("selectedPersonId")}).fetch();
cursor.forEach(function(doc){
for (i = 0; i < doc.examsubjects.length; i++) {
for (var prop in doc.examsubjects[i]) {
console.log("obj." + prop + " = " + doc.examsubjects[i][prop]);
var subj = doc.examsubjects[i][prop];
Told.insert({
examschoolid:"sms",
examname:doc.examname,
examsubjects: [{subj : "0"}],
examay:doc.examay,
examterm:doc.examterm,
examclass:doc.examclass,
examdate:doc.examdate
});
}
}
});
},
However, when the code runs, the variable subj
, which holds the subjects value, only inserts "subj" instead of recognizing it as a variable.
{
"_id" : "5yjwFanBAupgu9GHq",
"examschoolid" : "sms",
"examname" : "First",
"examsubjects" : [
{
"subj" : "0"
}
],
"examay" : "NrsP4srFGfkc5cJkz",
"examterm" : "5A5dNTgAkdRr5j53j",
"examclass" : "gYF2wE4wBCRy9a3ZC",
"examdate" : ISODate("2016-05-07T22:41:00Z")
}
Why is the variable not being interpreted as a variable?
Edit
'click .reactive-table tr': function() {
Session.set('selectedPersonId', this._id);
var cursor = Exam.find({ _id: Session.get("selectedPersonId")}).fetch();
cursor.forEach(function(doc){
var sq = function(){
for (i = 0; i < doc.examsubjects.length; i++) {
for (var prop in doc.examsubjects[i]) {
const subj = doc.examsubjects[i][prop];
let subject = {};
subject[subj] = "0";
return [subject];
}
}
}
console.log(sq());
Told.insert({
examschoolid:"sms",
examname:doc.examname,
examsubjects: sq(),
examay:doc.examay,
examterm:doc.examterm,
examclass:doc.examclass,
examdate:doc.examdate
});
});
//Uncaught TypeError: cursor.count is not a function
},
The updated code almost works, but it only inserts one record.