【问题标题】:Displaying elements from two different collections in meteor在流星中显示来自两个不同集合的元素
【发布时间】:2015-05-11 12:30:44
【问题描述】:

我有两个不同的流星集合,一个是用于存储图像的文件集合:

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

另一个是存储这些图像信息的集合:

Photos = new Mongo.Collection("photos");
Photos.attachSchema(new SimpleSchema({
  userId:{
    type: String,
    autoValue:function(){return this.userId},

  },
  userName:{
      type: String,
      autoValue:function(){return Meteor.users.findOne({_id: this.userId}).username},
  },

  groupMembers: {
    type: String
  },
  comments: {
    type: String,  
    autoform:{
        rows: 5
    }
  },
  fileId: {
    type: String
  }
}));

我构建了这个辅助函数来帮助在模板上显示数据:

Template.imagesSubmitted.helpers({
       photos: Photos.find(), 
       image: function(){Images.findOne({_id: Template.instance().data.image}).url();
        }
    });

Images.allow({
  download: function () {
    return true;
  },
  fetch: null
});

这里是显示图片和照片信息的 HTML 代码:

</template> 

<template name = "imagesSubmitted">
    List of photos
    {{#each photos}}
    <div class = "uploadedImage">  
      This is an image
      {{> photo}}
      <img src="{{this.image}}" class = "tableImage" alt="thumbnail">

    </div>    
    {{/each}}

</template>

不幸的是,该页面没有显示来自任一数据库的任何内容。图像仅显示为备用“缩略图”,{{&gt;photo}} 似乎没有显示照片集合中的任何信息。

有没有办法解决这个问题?还有一种方法可以简化这一点,以便我只使用一个集合并且仍然能够使用 cfs:autoform 创建和发布收集图像的表单?

【问题讨论】:

    标签: meteor meteor-autoform


    【解决方案1】:

    您是否尝试过在浏览器控制台中使用该查询?像 Photos.find().fetch(); 你是在定义照片模板吗?

    【讨论】:

    • 是的,看起来对象在那里:Photos.find().fetch() [Object, Object, Object, Object, Object, Object]
    • 那么照片模板呢……你定义了吗?
    • 不,但是当我删除它时,我仍然看不到 元素应该显示的图像。
    【解决方案2】:

    一定要允许下载功能。
    来自the docs

    Images.allow({
        download: function(userId, fileObj) {
            return true
        }
    })
    

    【讨论】:

    • 这就是我所拥有的:Images.allow({ download: function () { return true; }, fetch: null });
    • 你建议的参数我加了,没影响。
    • 嗯。检查您是否已发布和订阅它们并检查路径。
    • 我现在注意到的是:1)您正在为集合中的每个照片文档呈现一个{{&gt; photo}} 模板(不存在?)。如果模板不存在,这将不起作用。 2)&lt;img src="{{this.image}}"&gt; 具有来自Photos 收藏的照片文档的范围,我相信。所以它期望Photos 集合有image 字段..?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-19
    相关资源
    最近更新 更多