代码之家  ›  专栏  ›  技术社区  ›  Kristis

如何从Meteor CollectionFS中检索单个图像URL?

  •  0
  • Kristis  · 技术社区  · 10 年前

    我需要从CollectionFS中检索图像的URL。图像ID在帖子中被引用,因此首先我会找到属于某个帖子的图像,如下所示:

    模板:

    {{#each drafts}}
        <img src='{{images pictures.[0]}}'>
    {{/each}}
    

    帮手:

    images: function (id) {
        console.log(id);
        console.log((Images.findOne()));
        return Images.findOne({_id:id});
    }
    

    根据CollectionFS示例,您可以通过使用{{image.URL}}获取图像的URL,但在我的情况下{{images.URL pictures.[0]}}}不起作用,并向控制台返回手柄错误。在我的情况下,访问images.url数据的正确方法是什么?

    2 回复  |  直到 10 年前
        1
  •  2
  •   Marius Darila    10 年前

    在helper中,您可以这样调用:

    var image = Images.findOne({_id:id});
    return image.url();
    
        2
  •  1
  •   Ethaan    10 年前

    实际上,没有理由访问 URL 使用 Template.helper ,FSCollections配有 UI-helpers ,你可以这样使用。

    {{#each drafts}}
        URL: {{this.url}}
        <img src='{{this.url}}'>
    {{/each}}
    

    只需确保您有正确的下载许可

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

    供参考 如果您正在使用 findOne 没有可能使用 {{#each}} 你应该使用 {{#with}} 因为each helpers只接受数组和对象

    检查 Understanding SpaceBars