Search code examples
meteormeteor-autoformsimple-schema

How to add user id information in the schema and also hide form autoforms?


I am learning the ropes of Meteor and kind of lost here. I am using collections2, autoform for building my application. I want to store the collection along with user id information. So that when we retrieve the collection from the server, I want to show only the ones the user created not everything else. here is the schema.

    ExercisesSchema = new SimpleSchema({
    "name": {
        type: String,
        label: 'Name'
    },
    "workout.$.weight": {
        type: String,
        label: 'Weight'
    },
    "workout.$.reps": {
        type: String,
        label: 'Reps'
    },
    "notes": {
        type: String,
        label: 'Notes',
        optional: true
    }
});

on the server side I want to show only the workouts created by the user

Meteor.publish('exercises', function () {
    return Exercises.find({owner: this.userId});
});

When I added the user id to the schema, it shows up in the autoform, I am not sure how to hide it, if I hide it, then possibly I can use hooks in the autoform to add the values?


Solution

  • In the schema you can define the ownerId as type: "hidden"

    schema.js

    ExercisesSchema = new SimpleSchema({
        "name": {
            type: String,
            label: 'Name'
        },
        "ownerId": {
            type: String,
            autoform: {
                type: "hidden",
            }
        },
        "workout": {
            //not sure if you need this, you didn't have it in your
            type: [Object], 
            defaultValue: [] 
        },
        "workout.$.weight": {
            type: String,
            label: 'Weight'
        },
        "workout.$.reps": {
            type: String,
            label: 'Reps'
        },
        "notes": {
            type: String,
            label: 'Notes',
            optional: true
        }
    });
    

    And populate it with the hooks as you said.

    autoFormHooks.js

    AutoForm.hooks({
      exerciseForm: {
        formToDoc: function(doc) {
          doc.ownerId = Meteor.userId();
          return doc
        },
      }
    });
    

    An alternative to using hooks would be to use a quickFields inside of your autoForm for each field that you want to set in the doc, including ownerId. With this solution you would set the value of ownerId to currentUser.

    {{#autoForm collection="Exercises" id="exerciseForm" type="insert"}}
      <fieldset>
        <legend>Add an Exercise</legend>
        {{> afQuickField name='name'}}
        {{> afQuickField name='notes'}}
        {{> afQuickField name='ownerId' value=currentUserId}}
        {{> afQuickField name='workout'}}
      </fieldset>
      <button type="submit" class="btn btn-primary">Insert</button>
    {{/autoForm}}
    

    template.js

    Template.formTemplate.helpers({
        currentUserId: function () {
            return Meteor.userId();
        }
    });