【问题标题】:How to set a unique id for an embedded document in Meteor?如何为 Meteor 中的嵌入文档设置唯一 ID?
【发布时间】:2016-04-03 23:25:24
【问题描述】:

我已经使用简单模式设置了这样的集合:

SubLinkSchema = new SimpleSchema({
    name: {
        type: String,
        label: 'Link Name',
        unique: false
    },
    link: {
        type: String,
        regEx: SimpleSchema.RegEx.Url,
        label: 'Custom Link',
        optional: true,
        autoform: {
            class: 'sub-custom-link'
        }
    }

});

LinkSchema = new SimpleSchema({
    name: {
        type: String,
        label: 'Link Name',
        unique: false
    },
    link: {
        type: String,
        regEx: SimpleSchema.RegEx.Url,
        label: 'Custom Link',
        optional: true,
        autoform: {
            class: 'main-custom-link'
        }
    },
    subLinks: {
        optional: true,
        label: 'Sub Links',
        unique: false,
        type: [SubLinkSchema]
    }
});

在这里,问题是,子链接没有获得 ID。没有 id 很难更新它们。那么,如何为每个子链接(嵌入文档)生成唯一 ID?

【问题讨论】:

  • 查找Random.Id()
  • @Kyll 会不会是独一无二的?
  • 我同意MrE,应该使用SimpleSchema的“autoValue”,但是看看这个包:atmospherejs.com/mantarayar/shortid它非常好用,ids看起来像mongo ids。

标签: meteor meteor-autoform meteor-collection2 simple-schema


【解决方案1】:

在 SimpleSchema 中使用自动值字段

请参阅此处的参考: https://github.com/aldeed/meteor-collection2#autovalue

和例子:

subLinkID: {
    type: String,
    autoValue: function() {
        return Meteor.uuid();
    }
  }

【讨论】:

  • Meteor.uuid() 已被弃用。建议使用 Random.id() 代替。
【解决方案2】:

它应该与

Meteor.uuid()

【讨论】:

    猜你喜欢
    • 2012-04-26
    • 1970-01-01
    • 2014-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-20
    • 1970-01-01
    • 2013-05-17
    相关资源
    最近更新 更多