简体   繁体   中英

Meteor Mongo.Collection _id mismatch

I get props with the new inserts with _id's that don't exist on server at first. After this I get the inserts again with their correct _id's. The issue is that I use the ids from the first response to delete the events and the server doesn't find them, so it fails until the second response. Am I doing something wrong?

server

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

client

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);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM