sails.jsの入れ子モデル
質問
in in in in 0.10私は次の
をやろうとしています// user.js
module.exports = {
attributes: {
uuid: {
type: 'string',
primaryKey: true,
required: true
} ,
profile: {
firstname: 'string',
lastname: 'string',
birthdate: 'date',
required: true
}
}
};
.
ユーザーを作成しようとしていて、SailsJSが "profile"属性を認識していないときにエラーが発生しました。帆が入れ子になったJSON構造をサポートしているかどうかはわかりません。
error: Sent 500 ("Server Error") response
error: Error: Unknown rule: firstname
.
私は以下を試しましたが、それは失敗した
// user.js
module.exports = {
attributes: {
uuid: {
type: 'string',
primaryKey: true,
required: true
} ,
profile: {
firstname: {type: 'string'},
lastname: {type: 'string'},
birthdate: 'date',
required: true
}
}
};
.
私はSailsJS 0.10で "JSON"という属性があることを知っていますが、それがこのモジュールに合う方法はわかりません。
解決
ウォーターラインはネストしたスキーマの定義をサポートしていませんが、json
タイプを使用してモデル内の埋め込みオブジェクトを格納できます。だから、あなたはやるでしょう:
profile: {
type: 'json',
required: true
}
.
、次のようなユーザーインスタンスを作成できます。
User.create({profile: {firstName: 'John', lastName: 'Doe'}})
.
違いは、firstName
フィールドとlastName
フィールドが検証されないことです。埋め込みprofile
オブジェクトのスキーマがあなたが望むものと一致することを検証したい場合は、モデルクラスにbeforeValidate()
ライフサイクルコールバックを実装する必要があります。
attributes: {},
beforeValidate: function(values, cb) {
// If a profile is being saved to the user...
if (values.profile) {
// Validate that the values.profile data matches your desired schema,
// and if not call cb('profile is no good');
// otherwise call cb();
}
}
. 他のヒント
続く
所属していません StackOverflow