1

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);
                    });
                }
            }
        );
3
  • can you post your entryFormObj please ? Commented Mar 8, 2018 at 4:48
  • 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: [] }, }); Commented Mar 8, 2018 at 18:26
  • 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; } }); Commented Mar 8, 2018 at 18:28

2 Answers 2

1

From the documentation and the examples provided by Inserting and updating - MongoDB
the second argument for the insert callback is the object inserted and in your find you're looking for a document with result , it should be result._id , so this should work :

EntryForms.find({_id: result._id}, function(err, res) {
Sign up to request clarification or add additional context in comments.

2 Comments

I've logged the result from the insert callback and it returns the _id of the EntryForms object that was inserted. Attempting your solution produced the same error unfortunately.
hmm .. sorry that didn't work, but you said you attached the error message but i can't see it, maybe if you attach it you can get more help.
0

Turns out the issue had to do with the way in which I was publishing/subscribing to my object within Meteor. I registered my subscription in my router.js file and was then able to access my collection as expected. Chalk this one up to my small experience with Meteor.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.