Thursday 4 May 2017

Reading document's sub-field in mongoose that does not have a sub-field property returns an empty object

My collection Test schema is nested like:

Test: {
    name: String,
    subField: {
        name: String,
        ref: {type: ObjectId, ref: "Test"}
    }
}

I do a find like:

dbs.Test.find({})exec().then(function(collection){ ...

It returns all the documents. Then I access a field that has a name, but no subField name.

var field = collection[0];
field.name // returns name
field.subField // console.log is an empty object

It returns an empty object in the console. I assumed it would be null/undefined. What is field.subField actually returning if it does not have a subField name?



via htc_m8

No comments:

Post a Comment