node.js - sails.js nested models -
in sails.js 0.10 i'm trying following
// user.js module.exports = { attributes: { uuid: { type: 'string', primarykey: true, required: true } , profile: { firstname: 'string', lastname: 'string', birthdate: 'date', required: true } } };
i'm getting error when trying create user , sailsjs doesn't recognize "profile" attribute. i'm not sure if sails supports nested json structure, , if i'm not sure how structure it.
error: sent 500 ("server error") response error: error: unknown rule: firstname
i've tried following failed too
// user.js module.exports = { attributes: { uuid: { type: 'string', primarykey: true, required: true } , profile: { firstname: {type: 'string'}, lastname: {type: 'string'}, birthdate: 'date', required: true } } };
i know there attribute called "json" sailsjs 0.10, not sure how fit module.
waterline doesn't support defining nested schemas, can use json
type store embedded objects in model. so, do:
profile: { type: 'json', required: true }
and create user instances like:
user.create({profile: {firstname: 'john', lastname: 'doe'}})
the difference firstname
, lastname
fields won't validated. if want validate schema of embedded profile
object matches want, you'll have implement beforevalidate()
lifecycle callback in model class:
attributes: {}, beforevalidate: function(values, cb) { // if profile being saved user... if (values.profile) { // validate values.profile data matches desired schema, // , if not call cb('profile no good'); // otherwise call cb(); } }
Comments
Post a Comment