Help with cfs:gridfs

Hi guys!
Just getting started with Meteor. It’s amazing, but I’ve run into a couple of issues due to my noobness :smiley:

I’m struggling to implement user profile photos and get gridfs up and running. This is the error I’m getting:
Exception in template helper: TypeError: Cannot read property ‘image’ of undefined.

Here is the relevant code:

common.js

Template.registerHelper('getProfileImg', function (userId) {
  var imgUrl = UserImages.findOne({userId: userId}).image;
  return imgUrl;
});

posts.html

<img src="{{getProfileImg userId}}">

profile.js

Template.profile.events({
	'submit .edit-profile': function (event) {
		var file = $('#profileImage').get(0).files[0];

		if(file){
			fsFile = new FS.File(file);

			ProfileImages.insert(fsFile, function(err, result){
				if(err) {
					throw new Meteor.error(err);
				} else {

					var imageLoc = '/cfs/files/ProfileImages/'+result._id;

					UserImages.insert({
						userId: Meteor.userId(),
						username: Meteor.user().username,
						image: imageLoc
					});
					Router.go('/');
				}
			});
		}
		
		return false;
	}
});

Any help would ge greatly appreciated :smiley:

Cannot read property 'image' of undefined'. This means that UserImages.findOne({userId: userId}) is undefined. You can’t access properties or call methods on undefined things! JavaScript doesn’t like when you do that.

If you want an to get a url of a FS:File, the method you want is url, e.g., UserImages.findOne({userId: userId}.url();

I’m going through this course myself and fixed the issue by changing this line:
<img src="{{getProfileImg UserId}}">
Note the capital “U” in UserId. For some reason when I ran a find().fetch() on the UserImages Collection I saw it was a capital U.

Hope that helps!

1 Like

Thanks bud. I’ll give it a go!

I’ve just gone a bit further in and it’s incorrect, apologies. The actual change is in common.js:
Template.registerHelper(‘getProfileImg’, function(userId) {
var imgUrl = UserImages.findOne({UserId: userId}).image;
return imgUrl;
});