I am having trouble reading an Object property that I retrieved with MongoDB while working with MeteorJS. The object creation process is as follows:
header = ["name"];
values = ["word1", "word2"];
var tmp = {};
var data = new Array();
for (var cnt = 0; cnt < values.length; cnt++){
for (var i = 0; i < header.length; i++) {
temp[header[i]] = values[i];
}
data.push(temp);
After creating the object, I insert it into MongoDB using the following code:
for (obj in data) {
Badwords.insert(data[obj]);
}
However, when I try to access and read the property, the browser console shows inconsistencies:
Badwords.findOne()
Object {_id: "uNRNDtp3RGrKA6gWz", "name": "word1"}
Badwords.findOne().hasOwnProperty("name")
false
Badwords.findOne().hasOwnProperty('name')
false
Badwords.findOne().hasOwnProperty('\"name\"')
false
Badwords.findOne().hasOwnProperty('\'name\'')
false
Badwords.findOne().hasOwnProperty("\'name\'")
false
Badwords.findOne().hasOwnProperty("\"name\"")
false
Badwords.findOne()
Object {_id: "uNRNDtp3RGrKA6gWz", "name": "word1"}
Badwords.findOne()["name"]
undefined
On the other hand, when checking for "_id" property:
Badwords.findOne().hasOwnProperty("_id")
true
The issue may lie in the property name itself: "name"
instead of name
. Also, the keys
are undefined.
Badwords.findOne().keys
undefined
To address this, I created a method called getKey()
:
var myObj = Badwords.findOne()
undefined
myObj.getkey = function(obj){
var keys = [];
for(var key in obj){
keys.push(key);
}
return keys;
}
myObj.getkey(myObj)
["_id", "name", "getkey"]