插入 Meteor 中的 collection 时出错
Error when inserting to the collection in Meteor
我将流星与 SimpleSchema 和 Collection2 一起使用。并做出反应。我在向 collection 中插入项目时遇到错误。这是代码:
我的 collection 和 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);
我的方法代码在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,
});
}
});
在文件 AddRecipeForm.jsx 中,在组件的 handleSubmit 方法中,我获取了输入值(name 和 desc),然后调用 Meteor.call('recipes.insert', name, desc);
.我希望字段 Author 和 CreatedBy 使用 simple-schema autoValue.
在服务器上自动创建
但是当我尝试使用以下形式插入内容时总是出错:
insert failed: Error: Author is required
我试图将此代码添加到 recipe.insert 方法中:
let newRecipe = {
name,
description: desc,
}
RecipeSchema.clean(newRecipe);
Recipes.insert(newRecipe);
但这没有用。在官方 simple-schema 文档中,我发现没有必要:
NOTE: The Collection2 package always calls clean before every insert, update, or upsert.
我通过将 optional: true
添加到 RecipeSchema 中的字段 Author
和 CreatedAt
来解决这个问题。所以作者字段的代码是:
author: {
type: String,
optional: true,
autoValue: function() {
return this.userId;
},
},
但我不希望这个字段是可选的。我只想 autoValue
有效,并且此字段将填充正确的值。谁知道为什么会出现这个错误,如何解决?
更新
我注意到一个重要时刻。我在我的表格中插入了不同的收件人(我认为由于 optional: true
而无法正常工作)。当我 运行 meteor mongo
> `db.recipes.findOne()' 并得到不同的食谱时,我得到 objects 像这样:
meteor:PRIMARY> db.recipes.findOne()
{
"_id" : "RPhPALKtC7dXdzbeF",
"name" : "Hi",
"description" : "hiodw",
"author" : null,
"createdAt" : ISODate("2016-05-12T17:57:15.585Z")
}
所以我不知道为什么,但是 Author 和 CreatedBy 字段填写正确(作者:null 因为我还没有帐户系统)。但是这样一来,schema中的required和optinal是什么意思呢?我的解决方案(optional: true
)正确吗?
更新 2
又一个重要时刻!我从架构中删除了 author
字段。并从 createdBy
字段中删除了 optional:true
。它有效!没有可选的 true。我意识到实际问题出在架构的**作者字段*中。但问题是什么?
我猜这是内部框架问题。您的第一个代码看起来完全没问题。您可以 post 在 aldeed: collection2 github 存储库中 post 这个问题,而不是在这里 post 提出这个问题。维护它的有关人员会调查这个问题。
我将流星与 SimpleSchema 和 Collection2 一起使用。并做出反应。我在向 collection 中插入项目时遇到错误。这是代码:
我的 collection 和 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);
我的方法代码在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,
});
}
});
在文件 AddRecipeForm.jsx 中,在组件的 handleSubmit 方法中,我获取了输入值(name 和 desc),然后调用 Meteor.call('recipes.insert', name, desc);
.我希望字段 Author 和 CreatedBy 使用 simple-schema autoValue.
但是当我尝试使用以下形式插入内容时总是出错:
insert failed: Error: Author is required
我试图将此代码添加到 recipe.insert 方法中:
let newRecipe = {
name,
description: desc,
}
RecipeSchema.clean(newRecipe);
Recipes.insert(newRecipe);
但这没有用。在官方 simple-schema 文档中,我发现没有必要:
NOTE: The Collection2 package always calls clean before every insert, update, or upsert.
我通过将 optional: true
添加到 RecipeSchema 中的字段 Author
和 CreatedAt
来解决这个问题。所以作者字段的代码是:
author: {
type: String,
optional: true,
autoValue: function() {
return this.userId;
},
},
但我不希望这个字段是可选的。我只想 autoValue
有效,并且此字段将填充正确的值。谁知道为什么会出现这个错误,如何解决?
更新
我注意到一个重要时刻。我在我的表格中插入了不同的收件人(我认为由于 optional: true
而无法正常工作)。当我 运行 meteor mongo
> `db.recipes.findOne()' 并得到不同的食谱时,我得到 objects 像这样:
meteor:PRIMARY> db.recipes.findOne()
{
"_id" : "RPhPALKtC7dXdzbeF",
"name" : "Hi",
"description" : "hiodw",
"author" : null,
"createdAt" : ISODate("2016-05-12T17:57:15.585Z")
}
所以我不知道为什么,但是 Author 和 CreatedBy 字段填写正确(作者:null 因为我还没有帐户系统)。但是这样一来,schema中的required和optinal是什么意思呢?我的解决方案(optional: true
)正确吗?
更新 2
又一个重要时刻!我从架构中删除了 author
字段。并从 createdBy
字段中删除了 optional:true
。它有效!没有可选的 true。我意识到实际问题出在架构的**作者字段*中。但问题是什么?
我猜这是内部框架问题。您的第一个代码看起来完全没问题。您可以 post 在 aldeed: collection2 github 存储库中 post 这个问题,而不是在这里 post 提出这个问题。维护它的有关人员会调查这个问题。