1

Im trying to get the url of an uploaded image using a global template helper. So far i tried doing isPrimary.url() but i only get null, otherwise isPrimary.url returns a function. Any idea how to deal with this?

user_item.html

<template name="userItem">
  <div class="col-md-3">
    <div class="text-center">
      <img width="80" class="img-rounded" src="{{primaryPicture store='thumb'}}">       
    </div>
  </div>
</template>

global_templates.js

Template.registerHelper('primaryPicture', function () {
  var isPrimary = Images.findOne({userId: this._id, primary: true});
  if (isPrimary) {
    return isPrimary.url();
  } else {
    return '/user.png';
  }
});
Andrés Da Viá
  • 586
  • 1
  • 8
  • 24
  • CollectionFS loads the files asynchronously to the storage adapter. If `.url()` returns null that means the file is not completely uploaded yet. – Michel Floyd Sep 30 '15 at 03:51
  • It is uploaded. If i log isPrimary.url, then i get: function (options) { // // return rootUrlPathPrefix + baseUrl + area + '/' + self.collection.name + '/' + self._id + filename + queryString; } also isPrimary.url works in the template. But for some reason i cant return isPrimary.url() from the helper – Andrés Da Viá Sep 30 '15 at 04:13
  • @AndrésDaViá you could safe guard your helpers with `{{#if this.isUploaded}}` so the helper gets called only after it's uploaded –  Sep 30 '15 at 05:53
  • Didnt get that. Maybe an example would be better. Thanks! – Andrés Da Viá Sep 30 '15 at 06:07
  • In your Helper Code Please Check the value of `this._id` . Since its a global helper it is possible that it does not refer to User. – gatolgaj Sep 30 '15 at 10:16
  • this._id is referencing the user correctly – Andrés Da Viá Sep 30 '15 at 14:32
  • So.. isPrimary.isUploaded() returns true. I dont have any clue why isPrimary.url() returns null instead. i moved the validation logic into my template. Thats the only solution i came up with. – Andrés Da Viá Sep 30 '15 at 15:25

1 Answers1

0

I dont know why isPrimary.url(); returns null when it should return the original url, but fix it doing isPrimary.url({store:'thumb'});

Andrés Da Viá
  • 586
  • 1
  • 8
  • 24