MeteorJS:Autoform + CollectionFS,将来自FS.Collection的图像与相应的Mongo.Collection文档相关联?

Ras*_*sir 3 mongodb meteor mongodb-query meteor-autoform

我正在构建一个非常小的Meteor应用程序,只是为了更好地理解Autoform和CollectionFS,以及它们的结合使用.我目前使用以下软件包设置了所有内容:

iron:router, aldeed:autoform, aldeed:collection2, cfs:standard-packages,
cfs:filesystem, cfs:autoform
Run Code Online (Sandbox Code Playgroud)

我有一个示例Mongo Collection分配给"Books"设置了附加的SimpleSchema,其中包含了标题和作者等演示中的字段.文件上传的相应代码是:

fileId: {
  type: String,
  autoform: {
    afFieldInput: {
      type: "cfs-file",
      collection: "images"
    }
  }
} 
Run Code Online (Sandbox Code Playgroud)

FS.Collection代码是:

Images = new FS.Collection("images", {
 stores: [new FS.Store.FileSystem("images", {path: "~/uploads"})]
}); 
Run Code Online (Sandbox Code Playgroud)

这与quickform相关: {{> quickForm collection="Books" id="insertBookForm" type="insert"}}

插入很好,我可以迭代文档并使用空格键和一个名为"books"的辅助函数显示各个字段,如下所示:

{{#each books}}
    <li>{{title}} by {{author}}</li>
{{/each}}
Run Code Online (Sandbox Code Playgroud)

我还可以使用帮助程序迭代上传到FS.Collection的图像,返回称为"files"的整个集合,并循环遍历它们,如下所示:

{{#each files}}
  <img src="{{this.url}}" />
{{/each}}
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是将两者联系在一起.我希望能够做到以下几点:

{{#each books}}
  <li>{{title}}, by {{author}} <img src="The-Corresponding-Image}}" /></li>
{{/each}}
Run Code Online (Sandbox Code Playgroud)

显然不是那种确切的布局,但我基本上只是希望能够使用相应的标题和作者打印图像,以便能够根据我的需要使用autoform和collectionsfs.

我试图从Books集合中的特定文档中检索fileId,然后将其插入Images.findOne({fileId: fileId})并将两者连接在一起.

谁能指出我正确的方向?

Ras*_*sir 6

由于Ethaan的指导,我能够弄明白.我必须做的是以下内容:

Autoform Hook:

AutoForm.hooks({
 insertBookForm: {
   after: {
     insert: function(error, result, template) {
       insertedFile = Books.findOne(result).fileId;
       Images.update({_id: insertedFile}, {$set: {'book': result}});
     }
   }
 }
});
Run Code Online (Sandbox Code Playgroud)

_id在插入后立即将"book"字段设置为正在插入的文档(存储在result参数中).

这是我对应的HTML:

{{#each books}}
  <li>{{title}} by {{author}}</li>
    {{#with files}}
        <img src="{{this.url}}" />
    {{/with}}
{{/each}}
Run Code Online (Sandbox Code Playgroud)

我的助手们:

Template.layout.helpers({
  books: function () {
    return Books.find({});
  },
  files: function() {
    return Images.findOne({book: this._id});
  }
});
Run Code Online (Sandbox Code Playgroud)