Search code examples
meteormeteor-autoform

Get _id of updated object with Meteor Autoform hooks


Using an AutoForm hook, it is possible to capture the id of a newly created document. But with update, the hook returns '1' if the update is successful.

How can I access the _id of the document I am working on, so that I can route to a single document view after a successful update?

The code below is working for insert, but not for update

AutoForm.addHooks('articleForm', {
  after: {
    insert: function(error, result) {
      if (error) {
        console.log("Insert Error:", error);
      } else {
        console.log("Document inserted:", result);
        Router.go('showArticle', {_id: result });
      }
    },
    update: function(error, result) {
       console.log("this: " + this._id);
      if (error) {
        console.log("Update Error:", error);
      } else {
        console.log("Document updated: " + result);
        Router.go('showArticle', {_id: result });
      }
    }
  }
});

The console.logs show:

this: undefined
create_article.js:35 Document updated: 1
iron_core.js:62 pathFor couldn't find a route named undefined

Solution

  • I found the answer, thanks to this post: https://stackoverflow.com/a/29904625/993592

    The id is called docId, not _id.

    Router.go('showArticle', {_id: this.docId });