在Meteor中使用autoform将postId添加到注释中

时间:2021-07-09 04:01:51

How can I link add postId to comments when using meteor-autoform?

使用meteor-autoform时,如何将postId链接到注释?

I have tried

我努力了

AutoForm.hooks({
  insertCommentForm: {
    formToDoc: function(doc) {
      doc.postId = this.formAttributes.parentContext._id;
      return doc;
    },
  }
});

and

AutoForm.hooks({
  insertCommentForm: {
    formToDoc: function(doc) {
      doc.postId = Template.parentData(1)._id;
      return doc;
    },
  }
});

and

AutoForm.hooks({
  insertCommentForm: {
    before: {
      method: function(doc) {
        doc.postId = this.formAttributes.parentContext._id;
        return doc;
      }
    }
  }
});

and

AutoForm.hooks({
  insertCommentForm: {
    before: {
      method: function(doc) {
        doc.postId = Template.parentData(1)._id;
        return doc;
      }
    }
  }
});

but postId is undefined no matter what I do.

但无论我做什么,postId都是不确定的。

Edit

I use it like this:

我这样使用它:

<template name="comment">
  <div>
    <h1>{{_id}} {{title}}</h1>
    {{#if currentUser}}
      {{> quickForm collection="Comments" id="insertCommentForm" type="insert"}}
    {{/if}}
    ....

so _id should be accessible.

所以_id应该是可访问的。

Edit 2

Now I have tried

现在我试过了

before: {
  insert: function(doc, template) {
    doc.postId = Template.instance().post._id;
    console.log(doc);
    return doc;
  }
},

and in the template I use

并在我使用的模板中

{{> quickForm collection="Comments" id="insertCommentForm" type="insert" post=this template="bootstrap3-inline" label-class="sr-only"}}

but post is undefined so I get the error Uncaught TypeError: Cannot read property '_id' of undefined.

但是post是未定义的所以我得到错误Uncaught TypeError:无法读取未定义的属性'_id'。

1 个解决方案

#1


2  

Instead using your

而是使用你的

{{> quickForm collection="Comments" id="insertCommentForm" type="insert"}}

{{> quickForm collection =“Comments”id =“insertCommentForm”type =“insert”}}

just try

{{> quickForm collection="Comments" id="insertCommentForm" type="insert" postId=_id}}

{{> quickForm collection =“Comments”id =“insertCommentForm”type =“insert”postId = _id}}

and then try to access this value inside the helper via

然后尝试通过helper访问此值

Template.instance().data.postId


You also can send the whole post object to the sub-template like

您也可以将整个帖子对象发送到子模板

{{> quickForm collection="Comments" id="insertCommentForm" type="insert" post=this}}

{{> quickForm collection =“Comments”id =“insertCommentForm”type =“insert”post = this}}

and then have full access to that collection document via

然后通过以下方式完全访问该集合文档

(e.g.)

Template.instance().data.post._id


This is a small sample for accessing data through templates

这是通过模板访问数据的小样本

http://meteorpad.com/pad/Ke9DJnbvtsqjSHJy2/SimpleDataGivenThroughTemplates

#1


2  

Instead using your

而是使用你的

{{> quickForm collection="Comments" id="insertCommentForm" type="insert"}}

{{> quickForm collection =“Comments”id =“insertCommentForm”type =“insert”}}

just try

{{> quickForm collection="Comments" id="insertCommentForm" type="insert" postId=_id}}

{{> quickForm collection =“Comments”id =“insertCommentForm”type =“insert”postId = _id}}

and then try to access this value inside the helper via

然后尝试通过helper访问此值

Template.instance().data.postId


You also can send the whole post object to the sub-template like

您也可以将整个帖子对象发送到子模板

{{> quickForm collection="Comments" id="insertCommentForm" type="insert" post=this}}

{{> quickForm collection =“Comments”id =“insertCommentForm”type =“insert”post = this}}

and then have full access to that collection document via

然后通过以下方式完全访问该集合文档

(e.g.)

Template.instance().data.post._id


This is a small sample for accessing data through templates

这是通过模板访问数据的小样本

http://meteorpad.com/pad/Ke9DJnbvtsqjSHJy2/SimpleDataGivenThroughTemplates