0

I have a simple Mongoose object:

module.exports = mongoose.model('Flight', new Schema({ 
    name: String,
    nkr: Number,
    outbound: {
        nkr: Number,
        weight: Number,
        facilities: Array,
    }
});

And when I create a new blank flight, I get back data as follows:

"fligts": [{
    "_id": "55e8642d4666a2141019a372",
    "extra" :  {
        "facilities": []
     }
}]

This is inconvenient as if i run flight.outbound.length returns 1. I want a black outbound object if nothing is entered, is this possible?

Kivylius
  • 6,357
  • 11
  • 44
  • 71

1 Answers1

1

Mongoose creates empty array by default. So you need to explicitly set facilities field to undefined before saving

check this Mongoose creating empty arrays?

Community
  • 1
  • 1
vmkcom
  • 1,630
  • 11
  • 17