如何在Sails.js中嵌入和写入mongo对象(一级以上)? [英] How to embed and write mongo objects in Sails.js (more than one level deep)?

查看:94
本文介绍了如何在Sails.js中嵌入和写入mongo对象(一级以上)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从sails.js示例,



// Person.js

  var Person = {
属性:{
firstName:'STRING',
lastName:'STRING',
age:{
type:'INTEGER' ,
max:150,
required:true
}
birthDate:'DATE',
phoneNumber:{
type:'STRING',
defaultsTo:'111-222-3333'
}
emailAddress:{
type:'email',//电子邮件类型将被ORM
验证:必需:true
}
}
};

现在我如何添加emailAddress以将家庭和办公室作为嵌入字段?



尝试这样做:

  emailAddress:{{

work:{
type:'email',
},
personal:{
type:'email',
}
}
},

  emailAddress:{
属性:{

工作:{
类型:'email',
},
个人:{
类型:'电子邮件',
}
}
},

都不行。我得到错误,例如第二种情况的找不到属性的规则,第一种情况下出现意外的令牌{。

解决方案

p>好的,通过一些线程。
似乎Sails Waterline在此阶段不支持嵌入式MongoDB模式。
您可以编写自己的贡献或强制它,但开箱即用的支持(模型验证)等也需要被黑客入侵。
https://github.com/balderdashy/sails-mongo/issues/ 44



其他选项 - sails-mongoose不幸也不受支持。



更新
从V0.10开始,Sails是支持关联。也许这会使它工作。仍然是实验性的。



更新
通过关联功能,您可以强制使用不同模型中的模式并在它们之间创建引用,但到目前为止,您似乎无法嵌入它们,只能将它们与不同的集合/表相关联。 / p>

From sails.js example,

// Person.js

   var Person = {
      attributes: {
        firstName: 'STRING',
        lastName: 'STRING',
        age: {
          type: 'INTEGER',
          max: 150,
          required: true
        }
        birthDate: 'DATE',
        phoneNumber: {
          type: 'STRING',
          defaultsTo: '111-222-3333'
        }
        emailAddress: {
          type: 'email', // Email type will get validated by the ORM
          required: true
        }
      }
    };

Now how would I add emailAddress to have a home and office as embedded fields?

Tried to do it this way:

emailAddress: {   {

                        work: {
                            type: 'email',
                        },
                        personal: {
                            type: 'email',
                        }
                  }
             },

and

emailAddress: {  
                     attributes: {

                        work: {
                            type: 'email',
                        },
                        personal: {
                            type: 'email',
                        }
                  }
             },

both don't work. I get errors such as "No Rules found for attributes" for the second case, "Unexpected token { " in the first case.

解决方案

Ok, Following through some threads on this. It seems Sails Waterline has no support for embedded MongoDB schema at this stage. You can write your own contribution or force it, but the out of box support (model validation) etc. need to be hacked too. https://github.com/balderdashy/sails-mongo/issues/44

The other option - sails-mongoose is unfortunately not supported too. From where can we decide collection name in use of "sails-mongoose" package, in node.js + sailsjs?

Update. Starting with V0.10, Sails is supporting associations. Perhaps that will make it work. Still experimental.

Update. With the associations functionality you can force a schema in different models and create references between them, but so far it doesn't seem like you'll be able to embed them - only relate them from different collections/tables.

这篇关于如何在Sails.js中嵌入和写入mongo对象(一级以上)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆