Le Qs
Le Qs

Reputation: 805

Inserting a nested object in meteor

I have this document saved in my mongo collection called 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 trying to select data from this document and saving it into another using this code.The aim is to have the examsubjects value in the document above to be the key in the document i am inserting into.

'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
                });
            }
        }
    });
},

When the code runs,the variable subj that holds the subjects value just inserts subj not knowing its a variable like this

{
    "_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 seen 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 only inserts 1 record.

Upvotes: 1

Views: 372

Answers (2)

MasterAM
MasterAM

Reputation: 16488

This is because it is treated as a key in a literal object.

If you want to have subj's value as your key, you will need to use the bracket notation, creating the object beforehand:

const subj = doc.examsubjects[i][prop];
let subject = {};
subject[subj] = "0";

Told.insert({
    examschoolid:"sms",
    examname:doc.examname,
    examsubjects: [subject],
    ...
});

Upvotes: 2

aedm
aedm

Reputation: 6614

That's how JSON works, it takes keys literally. Fix it by using ES6 brackets notation:

examsubjects: [{
  [subj] : "0"
}],

Upvotes: 2

Related Questions