Ahmad
Ahmad

Reputation: 1931

sailsjs model property of type object, possible?

The following is my sails model, whenever I try to save() I get the following error

[Error: Unknown rule: address]

Use to have similar models with mongoose, without any issues, could anybody see any mistakes in my model?

Thanks!

/**
 * Users.js
 *
 * @description :: TODO: You might write a short summary of how this model works and what it represents here.
 * @docs        :: http://sailsjs.org/documentation/concepts/models-and-orm/models
 */

module.exports = {

  connection: 'MongoServer',

  attributes: {
       AcctSessionId: {type: 'string', required: false, default: ''},
       UserName: {type: 'string', required: true, unique: true},
       UserPassword: {type: 'string', required: true},
       AcctStartDelay: {type: 'string', required: false, default: ''},
       AcctStopDelay:{type: 'string', required: false, default: ''},
       AcctPlan: {type: 'string', required: false, default: ''},
       AcctResellerID: {type: 'string', required: false, default: ''},
       AcctPrice: {type: 'integer', required: false, default: ''},
       AcctCurrency: {type: 'string', required: false, default: 'LBP'},
       AcctEnabled: {type: 'boolean', required: false, default: true},
       AcctExpiry: {type: 'boolean', required: true},

      meta: {
        address: {type: 'string', required: false, default: ''},
        city: {type: 'string', required: false, default: ''},
        mobile: {type: 'integer', required: true, default: ''},
        email: {type: 'string', required: false, default: ''},
        name: {type: 'string', required: true, default: ''}
      }

  }
};

Upvotes: 4

Views: 1113

Answers (1)

dandanknight
dandanknight

Reputation: 659

You cannot nest data like that using Sails/Waterline ORM. The issue is with the meta : {} bit. So as far as sails is concerned, you have defined an attribute called meta, and are trying to apply a rule (such as type : 'string') called address, city, mobile, email etc, and these are not valid rules.

I suggest you either create a new model called address, or userMeta or something similar, and put address, city, mobile, email etc in there. Then associate them, or, just get rid of the meta : {} wrapper altogether and have it as such....

/**
* Users.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs        :: http://sailsjs.org/documentation/concepts/models-and-orm/models
*/

module.exports = {

   connection: 'MongoServer',

    attributes: {
        AcctSessionId: { type: 'string', required: false, default: '' },
        UserName: { type: 'string', required: true, unique: true },
        UserPassword: { type: 'string', required: true },
        AcctStartDelay: { type: 'string', required: false, default: '' },
        AcctStopDelay: { type: 'string', required: false, default: '' },
        AcctPlan: { type: 'string', required: false, default: '' },
        AcctResellerID: { type: 'string', required: false, default: '' },
        AcctPrice: { type: 'integer', required: false, default: '' },
        AcctCurrency: { type: 'string', required: false, default: 'LBP' },
        AcctEnabled: { type: 'boolean', required: false, default: true },
        AcctExpiry: { type: 'boolean', required: true },

        address: { type: 'string', required: false, default: '' },
        city: { type: 'string', required: false, default: '' },
        mobile: { type: 'integer', required: true, default: '' },
        email: { type: 'string', required: false, default: '' },
        name: { type: 'string', required: true, default: '' }
    }
};

See sails attributes for what you can or can't put in a model description

Upvotes: 2

Related Questions