【问题标题】:Get _id of updated object with Meteor Autoform hooks使用 Meteor Autoform 钩子获取更新对象的 _id
【发布时间】:2016-01-24 16:26:59
【问题描述】:

使用 AutoForm 挂钩,可以捕获新创建文档的 id。但是对于更新,如果更新成功,钩子会返回 '1'。

如何访问我正在处理的文档的 _id,以便在成功更新后路由到单个文档视图?

下面的代码适用于插入,但不适用于更新

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 });
      }
    }
  }
});

console.logs 显示:

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

【问题讨论】:

    标签: meteor meteor-autoform


    【解决方案1】:

    感谢这篇文章,我找到了答案:https://stackoverflow.com/a/29904625/993592

    id 称为 docId,而不是 _id

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

    【讨论】:

      猜你喜欢
      • 2019-02-14
      • 2015-03-09
      • 1970-01-01
      • 1970-01-01
      • 2015-10-06
      • 2016-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多