Viewing Uploaded image in meteor-files


#1

I am using https://github.com/VeliovGroup/Meteor-Files to upload the file.

I have “choices” collection which have each choice which containes a image each.
I have published the images file,

pub.js (server side)

Meteor.publish(‘files.images.all’, function () {
return Images.find().cursor;
});

main.html

{{#each choices}}
{{> choice}}
{{/each}}
{{name}}

main.js
Template.main.onCreated(function{
this.subscribe(‘Choices’);
});

Template.choice.onCreated(function(){
this.subscribe(‘files.images.all’);
});

Template.choice.helpers({
getImage(id) {
return Images.findOne({_id:id}).link();
}
});

I get Exception in template helper: getImage.

What I want is I have to display all the choices with their image & names.
I have the choices collections with data schema like
{name:“some choice”, featured_image:{ _id:“KcLkabv2wPiSwLpm6” }}
I am trying to get the link of the image by passing the ID to image collection.

How can I achieve this?
Is my approach is wrong?

I am new to meteor.


#2

Can you give more detail about the error?

Can you put a breakpoint in your helper function and test what Images.findOne returns?


How to output the image i uploaded using Meteor-Files?
#3

The above is the error I got.

When I console Images.findOne({_id:id}) It prints the Image object and link() prints the link too.

But when I return it shows exception.

I solved the problem,

I stored the Images.findOne({_id:id}) in a variable & returned it. It worked like charm.

My New Function here is,

Template.choice.helpers({
  getImage(id) {
  	path = Images.findOne({_id:id}).link();
  	return path;
  }
});

Finding this crazy solution took me a couple of days :smiley:
Thanks for your concern.