[英]Error when inserting to the collection in Meteor
I'm using meteor with SimpleSchema and Collection2. 我正在使用带有SimpleSchema和Collection2的流星。 And react. 并作出反应。 I faced with error when inserting an item to the collection. 向集合中插入项目时遇到错误。 here is the code: 这是代码:
my collection and schema in recipes.js: 我在recipes.js中的集合和架构:
import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { SimpleSchema } from 'meteor/aldeed:simple-schema';
export const Recipes = new Mongo.Collection('recipes');
Recipes.deny({
insert() { return true; },
update() { return true; },
remove() { return true; },
});
RecipeSchema = new SimpleSchema({
name: {
type: String,
},
description: {
type: String,
},
author: {
type: String,
autoValue: function() {
return Meteor.userId();
},
},
createdAt: {
type: Date,
autoValue: function() {
if(Meteor.isClient){
return this.userId;
} else if(Meteor.isServer){
return Meteor.userId();
}
},
}
});
Recipes.attachSchema(RecipeSchema);
My methods code in Methods.js 我的方法代码在Methods.js中
import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { SimpleSchema } from 'meteor/aldeed:simple-schema';
import { Recipes } from './recipes.js';
Meteor.methods({
'recipes.insert'(name, desc) {
new SimpleSchema({
name: { type: String },
desc: { type: String },
}).validate({ name, desc });
Recipes.insert({
name,
description: desc,
});
}
});
And in file AddRecipeForm.jsx in the handleSubmit method of the component I get the values of the inputs (name and desc) and then call Meteor.call('recipes.insert', name, desc);
在组件的handleSubmit方法的AddRecipeForm.jsx文件中,我获得了输入值(名称和desc),然后调用Meteor.call('recipes.insert', name, desc);
. 。 I want field Author and CreatedBy to create automatically on the server with simple-schema autoValue. 我希望使用简单模式autoValue在服务器上自动创建字段Author和CreatedBy。
But I have an error always when I am trying to inset something with the form: 但是,当我尝试插入表单时,总是出现错误:
insert failed: Error: Author is required 插入失败:错误:需要作者
I tried to add this code to the recipe.insert method: 我试图将这段代码添加到recipe.insert方法中:
let newRecipe = {
name,
description: desc,
}
RecipeSchema.clean(newRecipe);
Recipes.insert(newRecipe);
But that didn't work. 但这没有用。 And in official simple-schema docs I've found that is not necessary: 在官方的简单模式文档中,我发现这不是必需的:
NOTE: The Collection2 package always calls clean before every insert, update, or upsert. 注意:在每次插入,更新或更新之前,Collection2软件包始终调用clean。
I solved this problem as adding optional: true
to the fields Author
and CreatedAt
in my RecipeSchema . 我解决了这个问题,如添加optional: true
到田间地头Author
和CreatedAt
在我RecipeSchema。 so code for the author field is: 所以作者字段的代码是:
author: {
type: String,
optional: true,
autoValue: function() {
return this.userId;
},
},
But I don't want this fields to be optional. 但是我不希望这个字段是可选的。 I just want to autoValue
works and this fields will be filled with the right values. 我只想autoValue
起作用,并且此字段将填充正确的值。 Who knows why this error occurs and how to solve it? 谁知道为什么会发生此错误以及如何解决?
Update 更新资料
I noticed one important moment. 我注意到了一个重要时刻。 I inserted different recipies with my form (that i think is working wrong because of the optional: true
). 我在表单中插入了不同的代码(由于optional: true
,我认为工作不optional: true
)。 when I run meteor mongo
> `db.recipes.findOne()' and get different recipies, I get objects like these: 当我运行meteor mongo
>`db.recipes.findOne()'并获得不同的收件人时,我得到了如下对象:
meteor:PRIMARY> db.recipes.findOne()
{
"_id" : "RPhPALKtC7dXdzbeF",
"name" : "Hi",
"description" : "hiodw",
"author" : null,
"createdAt" : ISODate("2016-05-12T17:57:15.585Z")
}
So i don't know why, but fields Author and CreatedBy are filled correct (author: null beacuse i have no accounts system yet). 所以我不知道为什么,但是字段Author和CreatedBy正确填写了(作者:null,因为我还没有账户系统)。 but in this way, what is the meaning of required and optinal in schema? 但这样一来,是什么意思,并要求在架构optinal? is my solution (with optional: true
) correct? 我的解决方案( optional: true
)正确吗?
update 2 更新2
another important moment! 另一个重要的时刻! i removed author
field from the schema. 我从架构中删除了author
字段。 and removed optional:true
from the createdBy
field. 并从createdBy
字段中删除了optional:true
。 and it works! 而且有效! tithout optional true. tithout可选true。 I realized that actual problem is in **author field* of the schema. 我意识到实际的问题在模式的“作者字段”中。 but WHAT is the problem? 但是出什么问题了?
This is internal Framework issue, I guess. 我想这是内部框架问题。 Your First code looks perfectly alright. 您的第一个代码看起来很好。 Instead of posting this question here, you can post it on aldeed: collection2 github repository. 您可以在aldeed:collection2 github存储库中发布此问题,而不是在此处发布此问题。 The concerned people who maintain it will look into the issue. 维护它的有关人员将调查此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.