【问题标题】:How to embed and write mongo objects in Sails.js (more than one level deep)?如何在 Sails.js 中嵌入和编写 mongo 对象(超过一层深度)?
【发布时间】:2014-02-06 01:11:19
【问题描述】:

来自sails.js 示例,

// 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
        }
      }
    };

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

试图这样做:

emailAddress: {   {

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

和

emailAddress: {  
                     attributes: {

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

两者都不起作用。对于第二种情况,我收到诸如“没有为属性找到规则”之类的错误,在第一种情况下出现“意外的令牌 {”。

【问题讨论】:

  • 您可以在 Person 内部的 emailAddress 定义中使用 json 类型,然后使用注入器手动验证所需的结构。这可能会在未来通过水线解决,但在那之前这应该涵盖您的问题

标签: javascript node.js express sails.js


【解决方案1】:

https://github.com/balderdashy/sails-mongo/issues/44

似乎他们已经将其作为功能请求进行了计划。

【讨论】:

    【解决方案2】:

    好的,接下来通过一些线程。 Sails Waterline 目前似乎不支持嵌入式 MongoDB 模式。 您可以编写自己的贡献或强制它,但开箱即用的支持(模型验证)等也需要被破解。 https://github.com/balderdashy/sails-mongo/issues/44

    不幸的是,另一个选项 -sails-mongoose 也不支持。 From where can we decide collection name in use of "sails-mongoose" package, in node.js + sailsjs?

    更新。 从 V0.10 开始,Sails 支持关联。也许这会让它发挥作用。仍处于试验阶段。

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

    【讨论】:

    • Sails-mongo 不允许为嵌入文档指定架构
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-20
    • 2015-12-16
    • 2014-12-19
    • 1970-01-01
    • 1970-01-01
    • 2019-10-13
    • 1970-01-01
    相关资源
    最近更新 更多