我正在尝试使用单个文本字段将字符串插入到我的集合中。我收到了这个错误:
Exception while invoking method 'categories.insert' TypeError: func is not a function
at /var/www/node/lover/node_modules/simpl-schema/dist/doValidation.js:359:18
at Array.forEach (native)
at doValidation (/var/www/node/lover/node_modules/simpl-schema/dist/doValidation.js:358:17)
at ValidationContext.validate (/var/www/node/lover/node_modules/simpl-schema/dist/ValidationContext.js:217:57)
at [object Object].doValidate (packages/aldeed:collection2-core/collection2.js:399:33)
at [object Object].Mongo.Collection.(anonymous function) [as insert] (packages/aldeed:collection2-core/collection2.js:187:25)
at [object Object].Meteor.methods.categories.insert (server/methods.js:14:21)
at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1737:12)
at packages/ddp-server/livedata_server.js:719:19
at [object Object]._.extend.withValue (packages/meteor.js:1122:17)
这是我的架构:
import { Mongo } from 'meteor/mongo';
var Categories = new Mongo.Collection('categories');
const CategorySchema = new SimpleSchema({
title: {
type: String,
label: "Title",
max: 255,
optional: true
},
created: {
type: Date,
label: "Date Category Created",
autoValue: () => {
if(this.isInsert){
return new Date();
}
}
},
updated: {
type: Date,
label: "Date Category Modified",
autoValue: () => {
if(this.isUpdate){
return new Date();
}
}
}
});
Categories.attachSchema(CategorySchema);
export default Categories;
要调用的方法:
import { check } from 'meteor/check';
import { Categories, Elections, Nominees, Users, Votes } from '../collections';
Meteor.methods({
'categories.insert'(title){
check(title, String);
return Categories.insert({title: title});
}
});
然后我称之为:
...
Template.settings.events({
'click #newCategoryButton'(e){
let title = $("#newCategoryInput").val();
Meteor.call('categories.insert', title, (error, response) => {});
}
});
...
如果我在插入语句中添加{validate: false}
,我可以让它工作,这让我相信这是一个验证问题。我已经浏览了所有文档和一些博客和内容,但在使用collection2
和simple-schema
时,如果不是那种标准方式,则无法找到有关如何将数据插入我的集合的任何说明。实际上,如果你在collection2 documenation上向上滚动几行,插入调用的显示方式与我编写的基本相同。
我不想通过我的插入调用传递{validate: false}
,因为它首先破坏了向我的集合添加模式的目的。
急需帮助,坚持了好几个小时。