0

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+"&param="+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);
    }
  },
});

1 Answer 1

1

Use a ternary to check if there's a text property on the caption:

caption: (aArray[i].caption.text) ? aArray[i].caption.text : ''

Edit : If the caption property is questionable, then use the following:

caption: (aArray[i].caption) ? aArray[i].caption.text : ''

h/t RobG

Sign up to request clarification or add additional context in comments.

6 Comments

That won't stop the error as it still attempts to access aArray[i].caption.text. You probably meant: aArray[i].caption? aArray[i].caption.text : ''
OP said he expected an embedded object: "caption": {"id":"some id", "text":"some text"}, so I presumed the caption property will always be there, but on further inspection you're correct as that's the only time OP is using the caption prop.
@Data @RobG fell asleep and didn't get a chance to look at this last night, but thank you fro getting back to me. the caption property is always there, and the instances that are giving me trouble are "caption": null
Someone on a slack channel i'm on suggested creating an underscore.js mixin for the null value, so I may look into that as it is a more portable solution. But thank you for helping with the ternary operator option @Data
@pingo—exactly. So if caption: null then caption.text is going to throw can't get property text of null.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.