插入Meteor中的集合时出错(Error when inserting to the collection in Meteor)

编程入门 行业动态 更新时间:2024-10-27 11:20:14
插入Meteor中的集合时出错(Error when inserting to the collection in Meteor)

我正在使用meteor与SimpleSchema和Collection2。 并做出反应。 将项目插入集合时遇到错误。 这是代码:

我在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, }); } });

在组件的handleSubmit方法的文件AddRecipeForm.jsx中,我获取输入的值(name和desc),然后调用Meteor.call('recipes.insert', name, desc); 。 我希望字段Author和CreatedBy在服务器上使用简单模式autoValue自动创建。

但是当我试图用表格插入一些东西时,我总是有一个错误:

插入失败:错误:需要作者

我试图将此代码添加到recipe.insert方法:

let newRecipe = { name, description: desc, } RecipeSchema.clean(newRecipe); Recipes.insert(newRecipe);

但那没用。 在官方的简单模式文档中,我发现没有必要:

注意:Collection2包在每次插入,更新或upsert之前始终调用clean。

我解决了这个问题,因为在CreatedAt中添加了optional: true和字段Author和CreatedAt 。 所以作者字段的代码是:

author: { type: String, optional: true, autoValue: function() { return this.userId; }, },

但我不希望这些字段是可选的。 我只想要autoValue工作,这个字段将填充正确的值。 谁知道为什么会出现这种错误以及如何解决?

更新

我注意到一个重要的时刻。 我用我的表单插入了不同的收件人(我认为因为optional: true而工作错误)。 当我运行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") }

所以我不知道为什么,但字段Author和CreatedBy填写正确(作者:null因为我还没有帐户系统)。 但是这样,架构中必需optinal的含义是什么? 是我的解决方案( optional: true )正确吗?

更新2

另一个重要时刻! 我从架构中删除了author字段。 并从createdBy字段中删除了optional:true 。 它的工作原理! tithout可选true。 我意识到实际问题在于架构的**作者字段*。 但是问题是什么?

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?

最满意答案

我猜这是内部框架问题。 你的第一个代码看起来完全没问题。 您可以在aldeed:collection2 github存储库上发布此问题,而不是在此处发布此问题。 维护它的有关人员将调查这个问题。

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.

更多推荐

本文发布于:2023-07-08 00:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1070178.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Error   Meteor   collection   inserting

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!