当我尝试在将对象插入Mongo集合的客户端上使用Meteor方法时,我得到“TypeError:无法读取未定义的属性'_id'”。我正在使用Collection2-core和Validated Method。尝试调用该方法的客户端代码是:
import { submitNewEvent } from '../../../api/events/events';
const newEvent = {
eventName: "A String",
eventDescription: "A String",
eventPosition: {lat: 40, lng: -70},
};
submitNewEvent.validate(newEvent);
submitNewEvent.call(newEvent, (err, res) => {
if (err) {
console.log(err);
} else {
console.log(res);
}
});
方法/集合在模块imports / api / events.js中定义:
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import eventSchema from './eventSchema';
Events = new Mongo.Collection('events');
Events.attachSchema(eventSchema);
export const submitNewEvent = new ValidatedMethod({
name: 'submitNewEvent',
validate: eventSchema.validator(),
run({newEvent}) {
Events.insert(newEvent);
}
});
eventSchema本身不指定_id字段,但所有其他字段都会签出。奇怪的是,当我尝试在启动时从服务器直接插入同一个对象时,它工作正常,没有问题。什么可能导致该方法的问题?
整个错误文本是:
TypeError: Cannot read property '_id' of undefined
at Mongo.Collection.doValidate (http://localhost:3000/packages/aldeed_collection2-core.js?hash=18cc61915c0e22cac3180c5c8c9e0ac91bdd188a:372:33)
at Mongo.Collection.(anonymous function) [as insert] (http://localhost:3000/packages/aldeed_collection2-core.js?hash=18cc61915c0e22cac3180c5c8c9e0ac91bdd188a:259:25)
at DDPCommon.MethodInvocation.run (http://localhost:3000/app/app.js?hash=b893ac14f5773e1f67641becf8b78b71756ca168:948:16)
at ValidatedMethod._execute (http://localhost:3000/packages/mdg_validated-method.js?hash=0d0a63069b7327e1e04768f607f0db947dfe949d:130:45)
at DDPCommon.MethodInvocation.ValidatedMethod.connection.methods._connection$methods.(anonymous function) (http://localhost:3000/packages/mdg_validated-method.js?hash=0d0a63069b7327e1e04768f607f0db947dfe949d:90:21)
at http://localhost:3000/packages/ddp-client.js?hash=df770fd9a6a02fd730939b97d266ea2b12938e95:4088:25
at Meteor.EnvironmentVariable.EVp.withValue (http://localhost:3000/packages/meteor.js?hash=6d285d84547b3dad9717a7c89c664b61b45ea3d8:1090:15)
at Connection.apply (http://localhost:3000/packages/ddp-client.js?hash=df770fd9a6a02fd730939b97d266ea2b12938e95:4079:60)
at ValidatedMethod.call (http://localhost:3000/packages/mdg_validated-method.js?hash=0d0a63069b7327e1e04768f607f0db947dfe949d:103:32)
at MapComponent.newEventSubmitted (http://localhost:3000/app/app.js?hash=b893ac14f5773e1f67641becf8b78b71756ca168:415:28)