質問

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();
    }
}
.

他のヒント

続く

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top