Reputation: 125
I'm getting "TypeError: Cannot read property '_id' of undefined" when I attempt to use a Meteor Method on the client that inserts an object into a Mongo collection. I am using Collection2-core and Validated Method. The client side code that attempts to call the method is:
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);
}
});
And the Method/Collection is defined within the module 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);
}
});
The eventSchema itself doesn't specify an _id field but all the other fields check out. Curiously, when I try to do a straight insert of the same object into the Collection from the server on startup, it works fine and there's no problem. What could be causing this issue from the method?
The entire error text is:
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)
Upvotes: -2
Views: 798
Reputation: 125
Figured out what the answer was, thanks to a prompt by Buzz Moschetti! The method had angled brackets around newEvent, which itself was already an object, so the document was getting lost on its way to the collection.
Upvotes: 0