【问题标题】:Meteor collection2 is allowing numbers as stringsMeteor collection2 允许数字作为字符串
【发布时间】:2015-09-23 07:37:23
【问题描述】:

我正在开始一个新的 Meteor 项目并使用 Collection2 进行验证。我定义了以下架构。当我插入标题为 4 的文档时,我预计它会失败,因为我已将其指定为字符串。它没有失败。我怀疑 Meteor 的某些基本方面我没有得到。仅供参考,如果我省略标题,我会得到预期的错误。

我的架构:

Timestamps = new Mongo.Collection('timestamps');

var Schemas = {};

Schemas.Timestamp = new SimpleSchema({
  title: {
    type: String,
    label: "Title",
    max: 500,
    optional: false
  },
  notes: {
    type: String,
    label: "Notes",
    max: 1000,
    optional: true
  }
});

Timestamps.attachSchema(Schemas.Timestamp);

以下代码应该会失败,并显示标题需要是字符串的错误。但是,它并没有失败,并且该值被存储为字符串“4”。

创建时间戳:

Timestamps.insert({title: 4, comments: "a comment"});

这就是我发布和允许插入时间戳的方式。

Meteor.publish("timestamps", function() {
    return Timestamps.find();
});

Timestamps.allow({
    insert: function(timestamp) {
        return true;
    }
});

【问题讨论】:

    标签: meteor-collection2


    【解决方案1】:

    查看文档:

    https://github.com/aldeed/meteor-collection2#skip-conversion-of-values-to-match-what-schema-expects

    Collection2 有一个选项(默认为 true)来尝试匹配数据类型

    【讨论】:

      猜你喜欢
      • 2016-10-26
      • 1970-01-01
      • 2014-06-21
      • 2019-06-21
      • 2012-03-07
      • 2011-07-29
      • 2015-10-04
      • 2013-10-21
      • 1970-01-01
      相关资源
      最近更新 更多