在集合中使用关系

时间:2015-06-18 10:35:28

标签: meteor meteorite meteor-collection2 meteor-collections

我有一个名为Assesments的集合和另一个名为ChairAssesments的集合,我现在已经单独定义了这些集合,但是为了以后的使用,我想在Assesments中插入值时默认将值插入到ChairAssesments中。

所以我想做一些像

这样的事情
 Assesments.after.insert(function (userId, doc) {
  ChairAssesments.insert({ assesmentId: doc._id });
});

但这不起作用

enter image description here

评估收集

   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 个答案:

答案 0 :(得分:1)

集合的Insert方法将对象(JSON)作为第一个参数。在此处阅读有关JavaScript对象的更多信息:http://www.codermania.com/javascript/lesson/1r/objects

详细了解Meteor文档中的插入方法:http://docs.meteor.com/#/full/insert

您的代码应如下所示:

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