When cant a object be added to an object <p>Trying to add a value to a object. The object was generated from the start by a database request, containing a number of entires. A sample entry (say number 0) looks as follows:</p> <pre><code>{ _id: 55c8a069cca746f65c98369d, fulltext: 'Finance', alias: 'Finance', level: 0, fullurl: 'http://v-ghost.port0.org:808/dbfswiki/index.php/FMP', __v: 0, _parrent: [] } </code></pre> <p>Now I want to add an additional array to this object. An empty structure where there will be a number of other listings. I started by using <code>.push</code>, but the object does not have a <code>push</code> function. This one has done a few rounds, so latest version below:</p> <p>The structure I do want is:</p> <pre><code> { _id: 55c8a069cca746f65c98369d, fulltext: 'Finance', alias: 'Finance', level: 0, fullurl: 'http://v-ghost.port0.org:808/dbfswiki/index.php/FMP', __v: 0, _parrent: [] childs: [] } </code></pre> <p>I am not putting anything into childs at this point <em>(childs because children seems to reserved, and while it might work, don't really want to try changing it when the code is already broken)</em>, basically just adding an empty slot to put similar objects into as part of later processing. <strong>Really weird</strong> Changed the code as follows: console.log ("Root record now " + rootRecord); rootRecord.childs = 1; console.log("Alias is " + rootRecord.alias); console.log("Child is " + rootRecord.childs); console.log("The complete structure is: \n"+rootRecord)</p> <p>And I get the following output</p> <pre><code>Alias is Finance Child is 1 The complete structure is: { _id: 55c8a069cca746f65c98369d, fulltext: 'Finance', alias: 'Finance', level: 0, fullurl: 'http://v-ghost.port0.org:808/dbfswiki/index.php/FMP', __v: 0, _parrent: [] } </code></pre> <p>So I can read rootRecord.childs, but its not listed, its almost as it is a "hidden" variable somehow.</p> <p><strong>The offending code</strong></p> <pre><code> console.log("Creating root structure"); var roots = docs[doc]; console.log("Root from docs: \n" + docs[doc]) console.log("Sealed: " + Object.isSealed(docs[doc])); console.log("Frozen " + + Object.isFrozen(docs[doc])); console.log("Is Extendable: " + isExtendable(docs[doc])); console.log("Is Extensible(es6): " + Object.isExtensible(docs[doc])); for (var root in docs[doc]){ var rootRecord = docs[doc][root]; console.log ("Root record now " + rootRecord); rootRecord.childs = []; console.log("Now its " + rootRecord); returnStructure.push(rootRecord); console.log("returnStructure is now:\n" + returnStructure); console.log("And first id is " + returnStructure[0]['_id']) } </code></pre> <p>Gives the following output:</p> <pre><code>Creating root structure Root from docs: { _id: 55c8a069cca746f65c98369d, fulltext: 'Finance', alias: 'Finance', level: 0, fullurl: 'http://v-ghost.port0.org:808/dbfswiki/index.php/FMP', __v: 0, _parrent: [] } Sealed: false Frozen 0 Is Extendable: true Is Extensible(es6): true Root record now { _id: 55c8a069cca746f65c98369d, fulltext: 'Finance', alias: 'Finance', level: 0, fullurl: 'http://v-ghost.port0.org:808/dbfswiki/index.php/FMP', __v: 0, _parrent: [] } Now its { _id: 55c8a069cca746f65c98369d, fulltext: 'Finance', alias: 'Finance', level: 0, fullurl: 'http://v-ghost.port0.org:808/dbfswiki/index.php/FMP', __v: 0, _parrent: [] } returnStructure is now: { _id: 55c8a069cca746f65c98369d, fulltext: 'Finance', alias: 'Finance', level: 0, fullurl: 'http://v-ghost.port0.org:808/dbfswiki/index.php/FMP', __v: 0, _parrent: [] } And first id is 55c8a069cca746f65c98369d </code></pre> <p><strong>Full source code</strong></p> <pre><code>var mongoose = require("mongoose"); var morgan = require('morgan'); // log requests to the console (express4) var methodOverride = require('method-override'); // simulate DELETE and PUT (express4) var async = require('async'); mongoose.connect('mongodb://localhost/ResourceProfiles'); //var ObjectId = require('mongoose').ObjectID; var isExtendable = require('is-extendable'); var Schema = mongoose.Schema, ObjectId = Schema.ObjectId; var Profile = mongoose.model('Profile', { alias: String, img: String, name: String, summary: String, CV: String, keys: String, avail: String, agent: String, __v: {type: Number, select: false}, comp: { type: Number, default: 0 }, comn: { type: Number, default: 0 }, intp: { type: Number, default: 0 }, intn: { type: Number, default: 0 }, orgp: { type: Number, default: 0 }, orgn: { type: Number, default: 0 }, swep: { type: Number, default: 0 }, swen: { type: Number, default: 0 }, pssp: { type: Number, default: 0 }, pssn: { type: Number, default: 0 }, pep: { type: Number, default: 0 }, pen: { type: Number, default: 0 }, gtemp: { type: Number, default: 0 }, gtemn: { type: Number, default: 0 } }); var Skill = mongoose.model('Skill', { alias: String, fulltext: { type: String , required: true , unique: true }, fullurl: String, level: Number, _parrent: [{ type: ObjectId, ref: 'Skill'}], }) console.log("Lets see if we can't figure out this one once and for all"); var queries= []; var maxLevels = 1; [0,1,2,3,4].forEach(function(i){ console.log("Looking for "+ i) queries.push(function (cb) { console.log("Seaching for "+ i) Skill.find({level: i}).exec(function (err, docs) { if (err) { throw cb(err); } // do some stuff with docs &amp; pass or directly pass it cb(null, docs); }); }); }) console.log("All requests generated"); async.parallel(queries, function(err, docs) { // if any query fails if (err) { throw err; } var returnStructure = []; console.log("This is what we got back") for (var doc in docs){ if ( docs[doc].length === 0 ){ console.log("No entries in level " + doc) } else { console.log("Processing " + docs[doc].length + " for level " + doc) if ( doc === "0" ) { console.log("Creating root structure"); var roots = docs[doc]; console.log("Root from docs: \n" + docs[doc]) console.log("Sealed: " + Object.isSealed(docs[doc])); console.log("Frozen " + + Object.isFrozen(docs[doc])); console.log("Is Extendable: " + isExtendable(docs[doc])); console.log("Is Extensible(es6): " + Object.isExtensible(docs[doc])); for (var root in docs[doc]){ var rootRecord = docs[doc][root]; console.log ("Root record now " + rootRecord); rootRecord.childs = []; console.log("Now its " + rootRecord); returnStructure.push(rootRecord); console.log("returnStructure is now:\n" + returnStructure); console.log("And first id is " + returnStructure[0]['_id']) } /*} else if ( doc === "1"){ var skills = docs[doc]; for (var skill in skills){ console.log("Need to find " + skills[skill].alias + " parrent " + skills[skill]._parrent); for (var root in returnStructure) { if ( returnStructure[root]["_id"].toString() === skills[skill]["_parrent"].toString()){ console.log("Found parrent " + returnStructure[root].alias); var newSkill = []; var childs = { childs: {}}; newSkill.push(skills[skill]); newSkill.push(childs); console.log("This is it " + returnStructure); returnStructure[root].childs.push(newSkill); } } console.log(returnStructure); } } else if ( doc === "2"){ var skills = docs[doc]; for (var skill in skills){ console.log("Need to find " + skills[skill].alias + " parrent " + skills[skill]._parrent); for (var root in returnStructure){ //var parrents= returnStructure[root].childs; for (var parrent in returnStructure[root].childs){ console.log("Lets compare \n" + returnStructure[root].childs[parrent]._id + "\n" + skills[skill]._parrent); if( returnStructure[root].childs[parrent]._id.toString() === skills[skill]["_parrent"].toString() ){ console.log("Hello found " + returnStructure[root].childs[parrent].childs); skills[skill].childs = []; console.log(skills[skill]) returnStructure[root].childs[parrent].childs.push(skills[skill]) } } } } */ } // } } } }) </code></pre>