Reem
Reem

Reputation: 3

Can't insert or update after changing the schema - Meteor JS + Mongo

My collection and insert+update+delete functions were working properly until I changed the schema to include upVoters! None of the functions seem to work although they work fine before adding. I'm using aldeed simple schema and I don't get any errors. The problem is in the default value of the upVoters, because when I comment the default value everything runs smoothly. However, I need to keep track of every upVoter to allow them of voting once.

Any ideas? Thanks in advance!

import SimpleSchema from 'simpl-schema';
SimpleSchema.extendOptions(['autoform']);

Ideas = new Mongo.Collection('ideas');

Ideas.allow({
    insert: function(userId, doc) {
        return !!userId;
    },
    update: function(userId, doc) {
        return !!userId;
    }
});

IdeaSchema = new SimpleSchema({
    title: {
        type: String,
        label: "Title"
    },
    category: {
        type: String,
        label: "Category"
    },
    owner: {
        type: String,
        label: "Owner",
        autoValue: function() {
            return this.userId
        },
        autoform: {
            type: "hidden"
        }
    },
    createdAt: {
        type: Date,
        label: "Created At",
        autoValue: function() {
            return new Date()
        },
        autoform: {
            type: "hidden"
        }
    },
    upVoters: {
        type: Array,
        label: 'Up Voters',
        defaultValue: [this.userId],
        optional: true,
        autoform: {
            type: "hidden"
        }
    },
    'upVoters.$': {
        type: String
    },
});

Meteor.methods({
    deleteIdea: function(id) {
        Ideas.remove(id);
    }
});

Ideas.attachSchema( IdeaSchema );

Upvotes: 0

Views: 111

Answers (2)

Reem
Reem

Reputation: 3

In the version I'm suing, I can't declare Array as [type]... But when I removed the defaultValue line, the code worked!!!!

Upvotes: 0

Michel Floyd
Michel Floyd

Reputation: 20227

Array isn't a valid field type, you need an array of a type:

upVoters: {
    type: [String],
    label: 'Up Voters',
    defaultValue: [this.userId],
    optional: true,
    autoform: {
        type: "hidden"
    }
},

Upvotes: 1

Related Questions