bengott/meteor-avatar

Access Avatar.getUrl on server startup

Closed this issue · 7 comments

I'm attempting to access the Avatar.getUrl function when I populate my database on the server's Meteor.startup, but I'm getting a "Cannot call method 'getUrl' of undefined" error. Is the Avatar object only accessible client side? Any reason you can think of why this wouldn't be possible? Only thing I can think of is that the export.js file isn't in a lib directly, so it hasn't been loaded/defined yet when the Meteor.startup method is running.

I've tried to define Avatar.options in a directory accessible by both client & server, and get the same message. Is it possible to make it so that the Avatar object and helper methods can be used server side as well?

Just to provide more context.

I am trying to get the URL of the avatar and store it in the user object. This is because the ian:accounts-ui-bootstrap-3 package looks for a user_profile_picture property inside the user object, and if it's there, it will display that image for me in the navbar. So I figured that in the onCreaterUser callback, I could re-purpose the Avatar.getUrl method to fetch this URL to store in the user object.

I just now noticed that the getUrl method is accessing the window object, which would be undefined server side, so that pretty much answers my initial question of whether or not these functions could be made available server side (at least the current revision of it).

I am mentioning this just in case I am doing something really stupid and if you have a suggestion of a way to do it better. If not, I think making this package usable on the server is a good idea for an enhancement, assuming it is possible to refactor without dependencies on client-side-only objects (such as window). I am still rather new to javascript, so if all of this sounds like nonsense, that would be why :)

Hi Michael,

Thanks for your interest in the project. Yep, the Avatar package is currently only client-side, but I do have plans to make it available on the server as well. Because the package includes UI components like templates, there are some things that can't run on the server. When I first created the package, I tried to include server functionality but ran into some roadblocks, so I just decided to get it working client-side first. Since then, the code has been refactored and cleaned up a bit, so it shouldn't be too difficult to at least make the Avatar object/namespace accessible on the server. I'll see what I can do and get back to you.

I just pushed some changes that should make this possible. I haven't published it yet (still testing), but you can clone this repo if you want to test it out. Let me know if it's working for you.

Change almost works perfectly. There is one part where location still may be referred to on the server. In export.js, in getUrl(), on line 89, you set some options to get a Gravatar:

var options = {
          default: gravatarDefault || defaultUrl,
          size: 200, // use 200x200 like twitter and facebook above (might be useful later)
          secure: location.protocol === 'https:'
        };

On the server, if this method attempts to get a gravatar, the location object will be accessed, and an error is thrown. For my purposes, I just hard coded a false value and it worked.

I'm not sure what the proper solution is, though. I suppose another configuration value could be used? Maybe something like

secure: Avatar.options.useSecure || location.protocol === 'https:'

Thank you for your help so far!

Nice catch! I'll get that fixed..

Fixed. af5d835

BTW, I published the package with these changes. (version 0.3.0)