using relationship in collection

97 views Asked by At

I have a collection called Assesments and another collection called ChairAssesments, I have these defined separately now, but for later use I want to insert a value by default into ChairAssesments whenever I insert value in Assesments.

So I want to do something like

 Assesments.after.insert(function (userId, doc) {
  ChairAssesments.insert({ assesmentId: doc._id });
});

but this is not working

enter image description here

Assesment collection

   Assesments = new Mongo.Collection('assesments');

ChairAssesments = new Mongo.Collection('chairassesments');


Assesments.after.insert(function (userId, doc) {
  ChairAssesments.insert({ assesmentId: doc._id });
});

Assesments.before.insert(function (userId, doc) {
  doc.createdAt = new Date();
    doc.assesmentDate = new Date();
});

Assesments.attachSchema(new SimpleSchema({
  name: {
   type: String,
    label: 'First Name',
    autoform: {
      'label-type': 'placeholder',
      placeholder: 'First Name'
    }
  },
  email: {
    type: String,
    label: 'Email',
    autoform: {
      'label-type': 'placeholder',
      placeholder: 'Email'
  }
  },
  category: {
    type: String,
      label: 'Category',
    optional: true,
    autoform: {
      options: [
        {value: 'General', label: 'General'},
        {value: 'Reported', label: 'Reported'},
        {value: 'Follow Up', label: 'Follow Up'}
      ],
      type: 'select-radio'
    }
  },
 assesmentDate: {
    type: Date,
    label: 'Assesment Date',
    optional: true
  },
  location: {
    type: String,
      label: 'Location',
      autoform: {
      'label-type': 'placeholder',
      placeholder: 'Location'
    },
    max: 200
  },
    chairAssesments:{
        type: ChairAssesments
    }
  }
    ));

if (Meteor.isServer) {
  Assesments.allow({ 
    insert: function (doc) {
      return true;
    },
    update: function (doc, fieldNames, modifier) {
      return true;
    },
    remove: function (doc) {
      return true;
    }
  });
}

ChairAssesment collection

ChairAssesment = new Mongo.Collection('chairassesment');
ChairAssesment.before.insert(function (userId, doc) {
  doc.createdAt = new Date();
});


ChairAssesment.attachSchema(new SimpleSchema({
  assesmentId: {
    type: String
  },
    height: {
    type: String,
    label: 'Chair Height (Open hip angle)',
    optional: true,
    autoform: {
      options: [
        {value: 'Very Less', label: 'Very Less'},
        {value: 'Medium', label: 'Medium'},
        {value: 'Very High', label: 'Medium'}
      ],
      type: 'select-radio'
    }
  },
backSupport: {
    type: String,
    label: 'Back Support',
    optional: true,
    autoform: {
      options: [
        {value: 'Very Less', label: 'Very Less'},
        {value: 'Medium', label: 'Medium'},
        {value: 'Very High', label: 'Medium'}
      ],
      type: 'select-radio'
    }
  },
seatDepth: {
    type: String,
    label: 'Seat Depth',
    optional: true,
    autoform: {
      options: [
        {value: 'Very Less', label: 'Very Less'},
        {value: 'Medium', label: 'Medium'},
        {value: 'Very High', label: 'Medium'}
      ],
      type: 'select-radio'
    }
  },    
tiltLock: {
    type: String,
    label: 'Tilt Lock',
    optional: true,
    autoform: {
      options: [
        {value: 'Very Less', label: 'Very Less'},
        {value: 'Medium', label: 'Medium'},
        {value: 'Very High', label: 'Medium'}
      ],
      type: 'select-radio'
    }
  },
    armRests: {
    type: String,
    label: 'Arm Rests',
    optional: true,
    autoform: {
      options: [
        {value: 'Very Less', label: 'Very Less'},
        {value: 'Medium', label: 'Medium'},
        {value: 'Very High', label: 'Medium'}
      ],
      type: 'select-radio'
    }
  },
    fidgeting: {
    type: String,
    label: 'Fidgeting',
    optional: true,
    autoform: {
      options: [
        {value: 'Very Less', label: 'Very Less'},
        {value: 'Medium', label: 'Medium'},
        {value: 'Very High', label: 'Medium'}
      ],
      type: 'select-radio'
    }
  },
    standingUp: {
    type: String,
    label: 'Standing Up',
    optional: true,
    autoform: {
      options: [
        {value: 'Very Less', label: 'Very Less'},
        {value: 'Medium', label: 'Medium'},
        {value: 'Very High', label: 'Medium'}
      ],
      type: 'select-radio'
    }
  }
  }
    ));

if (Meteor.isServer) {
  ChairAssesment.allow({ 
    insert: function (doc) {
      return true;
    },
    update: function (doc, fieldNames, modifier) {
      return true;
    },
    remove: function (doc) {
      return true;
    }
  });
}
1

There are 1 answers

1
Tomas Hromnik On

Insert method of a collection takes object (JSON) as a first parameter. Read more about JavaScript objects here: http://www.codermania.com/javascript/lesson/1r/objects

Read more about insert method in Meteor docs: http://docs.meteor.com/#/full/insert

Your code should look like this:

Assesments.after.insert(function (userId, doc) {
  ChairAssesments.insert({ assesmentId: doc._id });
});