Meteor.user() in client/main.js returns undefined

1,263 views
Skip to first unread message

curiou...@gmail.com

unread,
May 15, 2013, 7:59:15 AM5/15/13
to meteo...@googlegroups.com
I am working through the discovermeteor book by Tom and Sacha. To learn things better I am trying out various things in another toy application (other than Microscope). 

I have the following package settings:

mrt remove autopublish
mrt add accounts-ui
mrt add accounts-password



In a file called main.js in the client folder, I added the following code:

var userobj = Meteor.user();
console.log("User object: ", userobj)

The userobj comes up as undefined. The user is logged in and I can access the user in the Templates using currentUser. For example, the code

{{#if currentUser}}
    <div>You are logged in.</div>
{{else}}
    <div>You should Log in.</div>
{{/if}}

in main.html, adds "You are logged in." to the page. 

My question is, why is Meteor.user() returning undefined in the main.js file. Is there a way for me to access Meteor.user() in the js files and condition code on whether the user is logged in, in the js files.

Thank you.

Thanks to the Meteor creators. I really hope to use it for some real projects. 

Petre Tudor

unread,
Nov 19, 2013, 4:57:38 PM11/19/13
to meteo...@googlegroups.com
Hi,

Did you manage to find a solution for your problem? I am facing something similar now.

Thanks,
Petre

Valerio Santinelli

unread,
Nov 20, 2013, 4:59:04 AM11/20/13
to meteo...@googlegroups.com
Of course you can access Meteor.users() in your client code, but you have to follow the publish/subscribe pattern to be able to read something from that collection.


Put your test app somewhere on Github if you want to let us have a look at the code to check what's wrong.

Regards,

Valerio

Kakajan SH

unread,
Jun 8, 2014, 6:47:11 PM6/8/14
to meteo...@googlegroups.com
I have the same problem ?? Did you  find a solution ?

Gabriel Pugliese

unread,
Jun 9, 2014, 1:39:03 PM6/9/14
to meteo...@googlegroups.com
I think you guys just need to wrap that code up into a Deps.autorun callback:

Deps.autorun(function () {
  var userobj = Meteor.user();
  console.log("User object: ", userobj)
});



Gabriel Pugliese
CodersTV.com
@coderstv


On Sun, Jun 8, 2014 at 7:47 PM, Kakajan SH <merva...@gmail.com> wrote:
I have the same problem ?? Did you  find a solution ?

--
You received this message because you are subscribed to the Google Groups "meteor-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to meteor-talk...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

BertC

unread,
Feb 24, 2015, 10:26:43 AM2/24/15
to meteo...@googlegroups.com
Same problem, still here.

Code:
Deps.autorun( function() {
   
var username = Meteor.user();
    console
.log("Username = [" + username + "]");
});

Result on console:
 Username = [[object Object]]

When done on console:
> Meteor.user()
Object {_id: "4QGbAuJfxZnK5x673", profile: Object, username: "bert2"}

> Meteor.user().username
"bert2"

Now changed the code to:
Deps.autorun( function() {
   
var username = Meteor.user().username;
    console
.log("Username = [" + username + "]");
});

And the result on the console:
Uncaught TypeError: Cannot read property 'username' of undefined

Weird? Strange? Bug? Or stupid JS-typo??
Help!!!!





Kelly Copley

unread,
Feb 24, 2015, 10:56:30 AM2/24/15
to meteo...@googlegroups.com
This an expected result.. First time the autorun executes there is a chance that the subscriptions will not have finished yet and thus Meteor.user() will return undefined. There are a couple options to fix this.. First you could guard your username like so..

Deps.autorun( function() {
    var user = Meteor.user();
    if(user){
        var username = user.username;
        console.log("Username = [" + username + "]");
    }
});

The other option that you have, which I actually consider to be much more favorable, is to add meteorhacks:fast-render to your application which will cause the initial user publication to be sent down with the inital html payload so by the time the autorun executes Meteor.user() will always return the logged in user.

As a note this issue can occur any time you have data that you subscribe to that may not be available when you try to retrieve it from minimongo, which is a good reason to always think about and guard your calls to findOne() calls.

--
You received this message because you are subscribed to the Google Groups "meteor-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to meteor-talk...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages