0

Hi I created a SimpleSchema for a Mongo collection which has a variable number of sub-documents called measurables. Unfortunately it's been a while since I've done this and I can't remember how to insert into this type of schema! Can someone help me out?

The schema is as follows:

const ExerciseTemplates = new Mongo.Collection('ExerciseTemplates');

const ExerciseTemplateSchema = new SimpleSchema({
  name: {
    type: String,
    label: 'name',
  },
  description: {
    type: String,
    label: 'description',
  },
  createdAt: {
    type: Date,
    label: 'date',
  },
  measurables: {
    type: Array,
    minCount: 1,
  },
  'measurables.$': Object,
  'measurables.$.name': String,
  'measurables.$.unit': String,
});

ExerciseTemplates.attachSchema(ExerciseTemplateSchema);

The method is:

Meteor.methods({
  addNewExerciseTemplate(name, description, measurables) {
    ExerciseTemplates.insert({
      name,
      description,
      createdAt: new Date(),
      measurables,
    });
  },
});

The data sent by my form for measurables is an array of objects.

The SimpleSchema docs seem to be out of date. If I use the example they show with measurables: type: [Object] for an array of objects. I get an error that the the type can't be an array and I should set it to Array.

Any suggestions would be awesome!!

Many thanks in advance!

edit:

The measurable variable contains the following data:

[{name: weight, unit: kg}]

With the schema above I get no error at all, it is silent as if it was successful, but when I check the db via CLI I have no collections. Am I doing something really stupid? When I create a new meteor app, it creates a Mongo db for me I assume - I'm not forgetting to actually create a db or something dumb?

4
  • Please add an example of measurables where not only the variable name is visible and also an excerpt of the error output. The schema looks ok so far. It seems to be rather a problem with the input data. Commented Jan 31, 2018 at 9:06
  • Thanks for the reply - I edited the original post to show the variable console.log Commented Jan 31, 2018 at 12:14
  • What happends, if you set the default clean option to false does it throw an error then? See: github.com/aldeed/simple-schema-js#set-default-cleaning-options Commented Jan 31, 2018 at 12:33
  • not at my dev machine at the moment but will try later and let you know how it goes. Commented Jan 31, 2018 at 13:17

1 Answer 1

0

Turns out I was stupid. The schema I posted was correct and works exactly as intended. The problem was that I defined my schema and method in a file in my imports directory, outside both client and server directories. This methods file was imported into the file with the form that calls the method, and therefore available on the client, but not imported into the server.

I guess that the method was being called on the client as a stub so I saw the console.log firing, but the method was not being called on the server therefore not hitting the db.

Good lesson for me regarding the new recommended file structure. Always import server side code in server/main.js!!! :D

Thanks for your help, thought I was going to go mad!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.