I'm using meteor with SimpleSchema and 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:
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
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);
. I want field Author and CreatedBy to create automatically on the server with simple-schema autoValue.
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:
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.
I solved this problem as adding optional: true
to the fields Author
and CreatedAt
in my 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. 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
). when I run meteor mongo
> `db.recipes.findOne()' and get different recipies, I get objects like these:
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). but in this way, what is the meaning of required and optinal in schema? is my solution (with optional: true
) correct?
update 2
another important moment! i removed author
field from the schema. and removed optional:true
from the createdBy
field. and it works! tithout optional 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. The concerned people who maintain it will look into the issue.