aviemet
aviemet

Reputation: 1653

SimpleSchema/Collection2 insert exception

I'm trying to get a single text field to insert a string into my collection. I'm getting this error:

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)

Here is my Schema:

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;

The method to be called:

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});
    }
});

And then I call it:

...
Template.settings.events({
    'click #newCategoryButton'(e){
        let title = $("#newCategoryInput").val();
        Meteor.call('categories.insert', title, (error, response) => {});
    }
});
...

I can get it to work if I add {validate: false} to my insert statement, which leads me to believe that it's a validation issue. I've gone through all the docs and a few blogs and tuts but can't find any instructions on how to insert data to my collection when using collection2 and simple-schema if it's not that standard way. In fact, if you scroll a few lines up on the collection2 documenation, the insert call is shown in basically the same form as I've written it.

I don't want to pass {validate: false} in with my insert calls as it defeats the purpose of adding a schema to my collections in the first place.

Desperate for help, been stuck on this for hours.

Upvotes: 1

Views: 242

Answers (0)

Related Questions