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

标签 mongodb meteor mongodb-query meteor-autoform

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

iron:router, aldeed:autoform, aldeed:collection2, cfs:standard-packages,
cfs:filesystem, cfs:autoform

我有一个分配给“书籍”的示例 Mongo 集合,设置了附加的 SimpleSchema,其中包含来自演示的字段,如标题和作者。文件上传对应的代码为:

fileId: {
  type: String,
  autoform: {
    afFieldInput: {
      type: "cfs-file",
      collection: "images"
    }
  }
} 

FS.Collection 代码是:

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

这与快速表单结合使用:{{> quickForm collection="Books"id="insertBookForm"type="insert"}}

插入没问题,我可以遍历文档并使用空格键和名为“books”的辅助函数显示各个字段,如下所示:

{{#each books}}
    <li>{{title}} by {{author}}</li>
{{/each}}

我还可以迭代上传到 FS.Collection 的图像,使用返回整个称为"file"的集合的助手,并像这样循环遍历它们:

{{#each files}}
  <img src="{{this.url}}" />
{{/each}}

我遇到的问题是将两者联系在一起。我希望能够按照以下方式做一些事情:

{{#each books}}
  <li>{{title}}, by {{author}} <img src="The-Corresponding-Image}}" /></li>
{{/each}}

显然不是那么精确的布局,但我基本上只是希望能够打印带有相应标题和作者的图像,以便能够使用 autoform 和 collectionfs 来满足我的需要。

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

谁能指出我正确的方向?

最佳答案

多亏了 Ethaan 的指导,我才能够弄明白。我必须做的是:

自动表单 Hook :

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

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

这是我对应的 HTML:

{{#each books}}
  <li>{{title}} by {{author}}</li>
    {{#with files}}
        <img src="{{this.url}}" />
    {{/with}}
{{/each}}

还有我的助手们:

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

关于mongodb - MeteorJS : Autoform + CollectionFS, 将来自 FS.Collection 的图像与相应的 Mongo.Collection 文档相关联?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28595170/

相关文章:

node.js - Mongodb atlas + node.js 在本地工作,但在推送到 Heroku 时停止

node.js - 查找未邀请任何用户的用户

android - meteor android sdk,是否可以安装在ubuntu vm(virtualbox)或windows上

node.js - MongoDB - 使用多个查找的 $group、辅助 $group、$sum 字段

mongodb - 如何分组 mongodb - mapReduce 输出?

mongodb - 如何删除mongodb中的数组元素?

MongoDB:如何找到已安装 MongoDB 的确切版本

ios - Meteor run ios - 根本不工作

javascript - underscorejs 将带冒号的字符串传递给它时,会根据环境呈现不同的结果

arrays - MongoDB:使用 Java 驱动程序拉取