使用MonogoDB的规范化数据模型显示实际的Image而不是ObjectId

时间:2015-01-19 17:59:58

标签: mongodb meteor

我想显示上传到MongoDB的图片 目前它显示ObjectId

enter image description here

Templates.coffee

Template.projectShow.helpers
  projects: ->
    Projects.find()

Projects.html

<template name="projectShow">
  <h2>Projects</h2>
     {{#each projects}}
        {{> showTemplate}}
     {{/each}}
</template>

<template name="showTemplate">  
  Title : {{title}} <br>
  Image : {{projectImage}}
</template>

Collections.coffee

@Projects = new Meteor.Collection('projects')
@imageStore = new FS.Store.GridFS("project-images")
@Images = new FS.Collection("project-images", stores: [imageStore])

Schemas.Projects = new SimpleSchema
  title:
    type: String

  projectImage:
    type: String
    autoform:
      afFieldInput:
      type: "fileUpload"
      collection: "Images"

Projects.attachSchema(Schemas.Projects)

1 个答案:

答案 0 :(得分:2)

您需要为showTemplate模板创建一个帮助函数,以查找文件集中所需的数据

projectImage返回存储在collectionsfs集合中的图像的id。因此,要获得实际的图像网址,您必须按照以下方式执行某些操作:(可能不是100%正确的语法,但只是为了解释流程)

Template.showTemplate.helpers({
    projectImage: function(){
        return Images.findOne({_id: Template.instance().data.projectImage}).url();
    }
});
相关问题