kuanb
kuanb

Reputation: 1708

Saving Model in Mongoose fails to save nested component

I have the following model schema:

var memberSchema = mongoose.Schema({

    'project'       : { 
        'type'      : Schema.Types.ObjectId, 
        'ref'       : 'Project' 
    },
    'first'         : String,
    'last'          : String,
    'email'         : String,
    'tracker'       : {
        'etag'      : String,
        'id'        : String,
        'photoLink' : String,
        'role'      : String,
        'type'      : String,
                      },
    'survey'        : {
        'etag'      : String,
        'id'        : String,
        'photoLink' : String,
        'role'      : String,
        'type'      : String,
                      },

});

I want to save a new document. I perform the following:

var Member     = require('../app/models/member');

var new_member = new Member({
  project      : project._id,
  first        : req.body.all_perms[i].first,
  last         : req.body.all_perms[i].last,
  email        : req.body.all_perms[i].email,
  tracker      : {
                    etag      : req.body.all_perms[i].etag_tracker,
                    id        : req.body.all_perms[i].ftid_tracker,
                    photoLink : req.body.all_perms[i].photoLink_tracker,
                    role      : req.body.all_perms[i].role_tracker,
                    type      : req.body.all_perms[i].type_tracker,
                 },
  survey       : {
                    etag      : req.body.all_perms[i].etag_survey,
                    id        : req.body.all_perms[i].ftid_survey,
                    photoLink : req.body.all_perms[i].photoLink_survey,
                    role      : req.body.all_perms[i].role_survey,
                    type      : req.body.all_perms[i].type_survey,
                 },
})
new_member.save(function(err, saved_result) {})

Performing this operation results in a successfully created new document BUT the document does not contain either tracker or survey. It does, however, contain first, last, and email.

How do I successfully save the nested component of the new model? Thanks.

Upvotes: 0

Views: 90

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 312035

To define a field in an embedded object named type, you need to use the object notation to define its type or Mongoose thinks it's defining the type of the parent object.

So change your schema to:

var memberSchema = mongoose.Schema({

    'project'       : { 
        'type'      : Schema.Types.ObjectId, 
        'ref'       : 'Project' 
    },
    'first'         : String,
    'last'          : String,
    'email'         : String,
    'tracker'       : {
        'etag'      : String,
        'id'        : String,
        'photoLink' : String,
        'role'      : String,
        'type'      : {'type': String},   // Here...
    },
    'survey'        : {
        'etag'      : String,
        'id'        : String,
        'photoLink' : String,
        'role'      : String,
        'type'      : {'type': String},   // ...and here
    },
});

Upvotes: 1

Related Questions