I'm working on an application using Meteor and MongoDB where I'm attempting use nested callbacks to access a newly inserted document as seen below. However I keep getting an error where there is no matching document in the database even though I'm in the successful callback of the insert statement. I'm not sure as to why Mongo can't find the document I just inserted. I understand that the methods are asynchronous, but I assumed that the callback would allow me to access the newly inserted document once the find function returns. I attempted to place the find outside of the insert statement with its own callback and got the same error.
I've attached the error message as well. Any help on this matter would be greatly appreciated!
insertEntryForm.call(entryFormObj, (error, result) => {
if (error) {
console.log(error);
toastr['error'](error.reason);
}
else {
toastr['success']("Entry form created!");
EntryForms.find({_id: result}, function(err, res) {
console.log(res);
});
}
}
);
entryFormObjplease ?EntryForms.schema = new SimpleSchema({ _id: { type: String, regEx: SimpleSchema.RegEx.Id }, event: { type: String }, email: { type: String, optional: true }, name: { type: String }, userId: { type: String, optional: true }, entries: { type: [Object], defaultValue: [] }, });export const insertEntryForm = new ValidatedMethod({ name: 'entryForms.insert', validate: EntryForms.simpleSchema().pick(['event', 'email', 'name', 'userId']).validator({clean: true, filter: false}), run({event, email, name, userId}) { if(!this.userId) { throw new Meteor.Error("entryForms.insert", "Not authorized to create new entry form"); } const entryForm = { event, email, name, userId, }; const entryFormId = EntryForms.insert(entryForm); return entryFormId; } });