[英]Meteor Mongo.Collection _id mismatch
我得到了帶有_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);
解決方案:從Events.schema中刪除_id: {type: String, optional: true},
從insertEvent中刪除_id: Random.id(),
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.