插入失败:错误:标题是必需的
我想添加一个对象到一个对象数组,这是一个集合项中的一个键,下面的代码,但我得到一个奇怪的响应“插入失败:错误:标题是必需的“。我在流星上使用简单的模式/ autoform。插入失败:错误:标题是必需的
有没有人遇到过这个(并有一个解决方案)?
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分配的标准,你在你的代码做的失误。
如果您的代码遭到黑客入侵并且没有分配任何标识而调用click事件会怎么样?
您的代码必须如下。
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: By default, all keys are required. Set
optional: true
to change that.
上面添加了相关的simpleschema,导致出现以下错误 'insert failed:Error:Content is required' – Silicabello
事件链现在很明显,它似乎试图为Posts创建一个新的添加项?我试图推送到Posts中现有条目中的invitesRequested值数组,为什么试图创建一个新的Posts条目?我应该使用不同的功能,而不是插入? – Silicabello
如果它帮助你实现你的答案,接受我的答案是一个简单的要求。接受答案激励我们回答越来越多的人,引导他们学习新的东西。 –
答案是使用Posts.update代替。但Ankur Soni的帖子引导我朝着正确的方向排除故障。
你的简单模式必须有一个标题是必需的 – Mikkel
是的,好像简单模式阻止插入,你需要允许标题。 – Deano
请在这里发布您的SimpleSchema。提供足够的细节,以免我们浪费时间在你的问题上。 –