插入失败:错误:标题是必需的



我正在尝试将一个对象添加到具有以下代码的集合条目中的键,但是我得到了一个奇怪的响应"插入失败:错误:错误:标题是必需的。我在流星上使用简单的模式/自动型。

有人以前遇到过这个问题(有解决方案)?

Template.dashboard.events({
  'click .requestinvite'(e,t) {
    Posts.insert({ _id : $(e.currentTarget).attr('_id')},
    {$push: { invitesRequested : {username : Meteor.userId()} }}
  );
}
});

这是CoffeeScript中相关的简单架构

Schemas.Posts = new SimpleSchema
    title:
        type:String
        max: 60
        optional: true
    content:
        type: String
        optional: false
        autoform:
            rows: 5
    createdAt:
        type: Date
        autoValue: ->
            if this.isInsert
                new Date()
    updatedAt:
        type:Date
        optional:true
        autoValue: ->
            if this.isUpdate
                new Date()
    invitesRequested:
        type: [Object]
        optional: true
        defaultValue: []

    owner:
        type: String
        regEx: SimpleSchema.RegEx.Id
        autoValue: ->
            if this.isInsert
                Meteor.userId()
        autoform:
            options: ->
                _.map Meteor.users.find().fetch(), (user)->
                    label: user.emails[0].address
                    value: user._id

首先根据适当的JavaScript分配标准,您在代码中犯了错误。

如果您的代码被黑客入侵,并且单击事件被调用,没有任何ID分配?

您的代码必须如下。

Template.dashboard.events({
  'click .requestinvite'(e,t) {
    var id = $(e.currentTarget).attr('_id');
    if(id){
    Posts.insert(
        { 
            _id : id
        },
        {   
            $push: { 
                    invitesRequested : {username : Meteor.userId()} 
                }
        }
    );
    } else {
        //do something here when you don't have id here, or the `click` event is hacked on UI to work without id'
    }
}
});

由于您的Simpleschema在title字段中给出了错误,如果不是强制性的,请在定义title字段时使用optional : true

,例如

title: {
    type: String,
    label: "Title",
    optional: true   //<---- do this
}

Note :默认情况下,需要所有键。将optional: true设置为更改。

答案是使用posts.update。但是Ankur Soni的帖子使我朝着正确的方向进行解决。

最新更新