Search code examples
meteorhandlebars.jsspacebarscollectionfs

How to retrieve single image URL from Meteor CollectionFS?


I need to retrieve an URL to an image from CollectionFS. Image IDs are referenced in posts so first off I find find an Image that belongs to a certain post like this:

Template:

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

Helper:

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

According to CollectionFS examples you get the URL to image by using {{image.url}}, but in my case {{images.url pictures.[0]}} does not work and return a handlebars error to console. What is the correct way in my case to access images.url data?


Solution

  • Actually there is no reason to access the URL using a Template.helper, FSCollections comes with a handily UI-helpers , and you can use it like this.

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

    Just be sure that you have the correct download allow

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

    FYI if you are using findOne there is not possible use {{#each}} you should use {{#with}} since the each helpers only accepts arrays and objects

    Check Understanding SpaceBars