javascript - Meteor - How to find out if Meteor.user() can be used without raising an error? -


i'm looking way determine if meteor.user() set in function can called both server , client side, without raising error when not.

in specific case use meteor server's startup function create dummy data if none set. furthermore use collection2-package's autovalue -functions create default attributes based on logged in user's profile, if available.

so have in server-only code:

meteor.startup(function() {     if (tags.find().fetch().length === 0) {         tags.insert({name: "default tag"});     } }); 

and in tags-collection's schema:

creatorname: {     type: string,     optional: true,     autovalue: function() {         if (meteor.user() && meteor.user().profile.name)             return meteor.user().profile.name;         return undefined;     } } 

now when starting server, if no tags exist, error thrown: meteor.userid can invoked in method calls. use this.userid in publish functions.

so in other words calling meteor.user() on server startup throws error instead of returning undefined or null or something. is there way determine whether prior calling it?

i cannot solve wrapping call if (meteor.isserver) within autovalue function, autovalue functions called server side when invoked user, , in these cases in code works fine.

note related how meteor.user() return on server side?, not address checking if meteor.user() available in cases calling might or might not result in error.

on server, meteor.users can invoked within context of method. makes sense won't work in meteor.startup. warning message is, unfortunately, not helpful. have 2 options:

try/catch

you can modify autovalue catch error if it's called wrong context:

autovalue: function() {   try {     var name = meteor.user().profile.name;     return name;   } catch (_error) {     return undefined;   } } 

i suppose makes sense if undefined acceptable name in dummy data.

skip generating automatic values

because know autovalue fail (and if didn't, won't add useful value), skip generating automatic values inserts. if need real name creator, pick random value existing database (assuming had populated users).


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -