Meteor Mongo.Collection _id不匹配

时间:2018-01-26 16:19:29

标签: javascript mongodb meteor

我获得了带有_id的新插件的道具,这些插件最初并不存在于服务器上。在此之后,我再次使用正确的_id进行插入。问题是我使用第一个响应中的ID来删除事件而服务器找不到它们,因此它会在第二个响应之前失败。我做错了吗?

服务器

export const Events = new Mongo.Collection('events');

if (Meteor.isServer) {
  Meteor.publish('events', function eventsPublication() {
    if (!this.userId) {
      return this.ready();
    }
    return Events.find({userId: this.userId});
  });
}


Events.schema = new SimpleSchema({
    _id: {type: String, optional: true},
    name: {type: String },
    groupId: {type: String },
    data: {type: Object, blackbox: true, optional: true}
});

export const insertEvent = new ValidatedMethod({
    name: 'events.insert',
    validate: new SimpleSchema({
        name: {type: String},
        groupId: {type: String },
        data: {type: Object, blackbox: true, optional: true}
    }).validator(),
    run(data) {

        if (! Meteor.userId())  throw new Meteor.Error('not-authorized');

        let newEvent = {
        _id: Random.id(),
        name: data.name,
        groupId: data.groupId,
        userId: Meteor.userId(),
        data: data.data || {}
        }

        check(newEvent, Events.schema);
        Events.insert(newEvent);
    },
});

export const removeEvent = new ValidatedMethod({
    name: 'events.remove',
    validate: new SimpleSchema({
        _id: {type: String}
    }).validator(),
    run(data) {

        let event = Events.findOne(data._id);
        if(!event) throw new Meteor.Error('event-not-found', data._id);

        if (!Meteor.userId() || event.userId != Meteor.userId())  throw new Meteor.Error('not-authorized');

        Events.remove(data._id);
    }
});

客户端

export default withTracker(props => {
  const vHandle = Meteor.subscribe('views');
  const eHandle = Meteor.subscribe('events');
  const view = Views.findOne(props._id || props.viewId);
  return {
    ready: vHandle.ready() && eHandle.ready(),
    events: Events.find({groupId: props.groupId}).fetch(),
    ...view
  };
})(View);

1 个答案:

答案 0 :(得分:0)

解决方案:从Events.schema中删除_id: {type: String, optional: true},,从insertEvent中删除_id: Random.id(),