I am parsing an array of objects, and there's about 12 fields in each. I am doing this with two nested for(var i = 0; i < array.length; i++) functions, and ran into a null field several responses in.
I am expecting to get an embedded object, ... "caption": {"id":"some id", "text":"some text"}, but am instead getting a null in some cases. My schema does not require the field to have a value, but the document is getting kicked out.
How can I get around this? Would expect the null just to insert a blank value, but that is not the case. I am working within the Meteor.js framework, but the snippet I need help with below is just plain old javascript and a mongodb upsert. The error is on the caption.text line.
Meteor.methods({
'getMethod': function (var1, var2) {
check (var1, Number);
this.unblock();
var url = "https://www.url.com/"+var1+"/stuff?"+token+"¶m="+var2;
var result = HTTP.call("GET", url);
if(result.statusCode===200) {
var aArray = JSON.parse(result.content).data;
for(var i = 0; i < aArray.length; i++){
var id = aArray[i].id;
var aItem = {
_id: aArray[i].id,
userId: aArray[i].user.id,
username: aArray[i].user.username,
created_time: parseInt(aArray[i].created_time),
type: aArray[i].type,
caption: aArray[i].caption.text, // THIS LINE IS THROWING THE ERROR !!
}
Collection.upsert(id, {$set: aItem}, {validationContext: 'upsertForm'}, function(error, result) {
if (error){
console.log("Error:", error);
return aArray;
}
});
}
} else {
console.log("Error: ", result.statusCode);
var errorJson = JSON.parse(result.content);
throw new Meteor.Error(result.statusCode, errorJson.error);
}
},
});