Search code examples
meteorsimple-schema

Combining simple schema errors


I'm trying to combine fields using simple schema. It works for Schema.UserProfile however it doesn't work for Schema.accountStatus.

If I try and populate the field when I create a new account it errors. Any thoughts on why, would really help, thanks?

Path: schemas.js

Schema = {};

Schema.accountStatus = new SimpleSchema({
    isUserAccountActive: {
        type: Boolean,
        optional: true
    },
    startDate: {
        type: Date,
        optional: true
    },
    endDate: {
        type: Date,
        optional: true
    }
});

Schema.UserProfile = new SimpleSchema({
    firstName: {
        type: String,
        optional: false
    },
    lastName: {
        type: String,
        optional: true
    },
});

Schema.User = new SimpleSchema({
    profile: {
        type: Schema.UserProfile,
        optional: true
    },
    // Make sure this services field is in your schema if you're using any of the accounts packages
    services: {
        type: Object,
        optional: true,
        blackbox: true
    },
    accountStatus: {
        type: Schema.accountStatus,
        optional: true

    },
    // In order to avoid an 'Exception in setInterval callback' from Meteor
    heartbeat: {
        type: Date,
        optional: true
    }
});

Meteor.users.attachSchema(Schema.User);

Path: startup.js

Meteor.startup(function () {

  console.log('Running server startup code...');

  Accounts.onCreateUser(function (options, user) {
    if (options.profile && options.profile.roles) {
      //include the user profile
      Roles.setRolesOnUserObj(user, options.profile.roles);
    }

    if (options.profile) {
      // include the user profile
      user.profile = options.profile;
    }

    // other user object changes...
    // ...
    user.isUserAccountActive = false;


    return user;
  });

});

Solution

  • I see now: isUserAccountActive is a sub-key of accountStatus. Change:

    user.isUserAccountActive = false;
    

    to

    user.accountStatus = { isUserAccountActive: false };
    

    Not clear why that should yield a server error though, perhaps it's because you're doing this update on the server side.