【问题标题】:Datatype validation error - MeteorJS/Autoform/SimpleSchema/Embedded Simple Schema数据类型验证错误 - MeteorJS/Autoform/SimpleSchema/Embedded Simple Schema
【发布时间】:2015-05-02 14:24:38
【问题描述】:

我在 Meteor 中使用 Simple Schema、collection hooks 和 Autoform 包,并且我试图在 after update 集合 hook 中更新我的模式中的 Embedded 对象。我觉得我可能会做一些愚蠢的事情,但就是无法解决这个问题。我在保存时得到了例外:Exception while invoking method '/providers/update' Error: 0 must be an integer 我的架构:

Schemas.Providers = new SimpleSchema({
email: {
type: String,
label: 'Email Address',
autoValue: function() {
  if (this.isInsert || this.isUpdate) {
    return Meteor.user().emails[0].address;
  }
},
autoform: {
  afFieldInput: {
    type: 'email'
  }
 }
},
officelocation: {
 type: String,
 label: 'Location of Office',
 optional:true
},
location: {
 type: [LocationSchema],
 optional:true
}});

有效的收集钩子:

Providers.after.update(function (userId, doc) {
var oldDoc = this.previous;
Providers.direct.update({_id: doc._id},{$set: {location:{
    type:'Point',
    coordinates:[0,0]
}}});
});

集合钩子不起作用。理想情况下,我不应该在集合更新后更新,但想确保它有效:

Providers.after.update(function (userId, doc) {
var oldDoc = this.previous;
var array=[];
var iArray=doc.officelocation.split(",");
array.push(Number(iArray[1]));
array.push(Number(iArray[0]))
Providers.direct.update({_id: doc._id},{$set: {location:[{
    type:'Point',
    coordinates:array
}]}}); 
});

【问题讨论】:

    标签: meteor meteor-autoform meteor-collection2


    【解决方案1】:

    查看您要存储的内容,使用 parseInt 而不是您正在使用的 Number。它将返回一个 mongo 可以存储的整数。


    问题不在于您使用的方法。这与您在 mongo 中存储数据的方式有关。向我们展示您的 LocationSchema 的外观。

    详情:

    Mongo 使用与 javascript 不同的数字格式。在 javascript 中,数字可以是整数、浮点数、小数或任何你想要的。 Mongo 对整数或浮点数有非常严格的要求。

    总的来说,这意味着如果您想在 mongo 中存储一个准确的十进制数(我怀疑您正在尝试做什么),您必须将其存储为字符串(您失去了直接执行的能力mongo 操作如 $inc $gt 等)或将其分成两部分并分开存储。第三种选择是将其存储为不准确的浮点数,仅当您想要某种近似值时才有用。

    【讨论】:

    • 我的位置模式看起来像这样。你是对的,我希望存储经纬度,所以我需要准确地存储它。 LocationSchema = new SimpleSchema({ type : { type : String, autoValue: function() { return "Point"; } }, coordinates: { type: [Number] } });
    • 另外,我不能将其存储为字符串,因为我正在使用 Mongo 的 geoWithin cpabilities 在我的应用程序中进行一些位置搜索..
    • 如果有人感兴趣,这个问题可以通过在简单模式中添加decimal: true 选项来解决。 {{> afQuickField name="decimal" step="0.01"}}
    猜你喜欢
    • 2014-07-21
    • 1970-01-01
    • 2016-01-08
    • 1970-01-01
    • 2017-12-20
    • 2015-12-31
    • 2020-09-26
    • 2015-10-18
    • 2015-06-23
    相关资源
    最近更新 更多