0

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

所以我想做类似的事情

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

但这不起作用

在此处输入图像描述

评估收集

   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 = 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;
    }
  });
}
4

1 回答 1

1

集合的插入方法将对象 (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 });
});
于 2015-06-18T11:04:35.557 回答