Meteor.user() undefined after adding react-router

Hello,

I’m building my first Web app with Meteor and React. There is not much functionality in it. A user can create an account and log in and out. Yesterday I wanted to get the email address of the user who is logged in, but every time when I want to user Meteor.user(), it’s undefined. A couple days ago it did work, when the page loaded it took like a second and Meteor.user() returned me the information. BUT if a user logs in, after he clicks on the link in the verification email, Meteor.user() returns the correct data. If the user reloads the page Meteor.user() is undefined.

The only change I did since then is, I added some routing to the project, react-router.

I uploaded the project to GitHub, if anyone want to have a look.

Thank you

I tried it, but could not register a new user. Every time I used the verification link, it was “expired”. Not sure if the Blaze-based login stuff will work in combination with React Router. I am using this package:

but since last month, it shows a deprecation notice :-/

Ok, I solved it, by using withTracker.

export default withTracker(() => {
  Meteor.subscribe("projects");

  return {
    projects: ProjectsDB.find({owner: Meteor.userId()}, { sort: { createdAt: -1 } }).fetch(),
    currentUser: Meteor.user(),
  };
})(Projects);

The problem you were dealing with was a race condition, which is common when you are working with code that runs asynchronously. Basically you were accessing the value of something before the network operation to fetch that value was completed.

2 Likes