【问题标题】:Embedded Schema Inserting Blank in Meteor using Collection2使用 Collection2 在 Meteor 中插入空白的嵌入式模式
【发布时间】:2019-08-18 05:23:08
【问题描述】:

我对流星相当陌生,并尝试使用使用嵌入式模式的模型插入集合。嵌入模式中的内容没有被插入到数据库中,而是一个空条目。

主模型正在附加到集合中。

Guests = new Mongo.Collection('guests');

Schema = {}

Guests.attachSchema(new SimpleSchema({
    BasicInformation : {
        type: Schema.basicInfo,
        optional: false,
    },
})

basicInfo 架构定义如下。

Schema.basicInfo = new SimpleSchema({
    firstName: {
        type: String,
    },
    middleName: {
        type: String,
    },
    lastName: {
        type: String,
    }
})

我正在使用它来插入一个通用 js 文件的集合中。

Guests.insert({
    BasicInformation: {
        firstName: 'First Name',
        middleName: 'Middle Name', 
        lastName: 'Last Name'
    },
})

如果我删除架构并在主模型中添加字段而不是使用嵌入式架构,那么它确实会被插入。不知道发生了什么……求助!

【问题讨论】:

  • 请编辑您的问题,以便将代码添加为格式化文本(请避免通过 cmets 添加代码)。发布在图片中的代码难以阅读,人们倾向于对图片中的代码问题投反对票。
  • 您介意分享解决方案吗?
  • @Mikkel 在他们的回复中提供了解决方案

标签: javascript html meteor schema meteor-collection2


【解决方案1】:

欢迎来到 Stack Overflow。而且,正如@Jankapunkt 所说,请将您的代码作为格式化块放在您的问题中。如果图像被删除,指向其他地方托管的图片的链接可能无法使用。我们也更容易修复您的代码并向您展示它的外观。

我认为在您设置架构时,架构对象是空的。您稍后会向其中添加信息,但此时为时已晚。如果你把代码放在你的问题中,我可以告诉你怎么做,但我不愿意为你重新输入。

更新: 干得好。您需要在 将其附加到表之前填充 Schema 对象:

Guests = new Mongo.Collection('guests');

Schema = {} // Right now the object is empty

Schema.basicInfo = new SimpleSchema({ // So we add the sub-schema
    firstName: {
        type: String,
    },
    middleName: {
        type: String,
    },
    lastName: {
        type: String,
    }
})

Guests.attachSchema(new SimpleSchema({ 
    BasicInformation : {
        type: Schema.basicInfo, // previously this was undef, now it is correct
        optional: false,
    },
})

这应该适合你。

【讨论】:

  • 有道理,我已经修复了我的帖子!
猜你喜欢
  • 1970-01-01
  • 2014-05-09
  • 1970-01-01
  • 2012-11-04
  • 2021-01-10
  • 2014-02-19
  • 2011-03-01
  • 1970-01-01
  • 2021-08-26
相关资源
最近更新 更多