Sylar
Sylar

Reputation: 12072

Meteor's find method only works in Chrome

What is wrong with my code. Meteor.users.findOne() or Meteor.user.find() only works in Chrome. Does not work in Firefox nor Safari -- Im on a Mac. My error is:

TypeError: Meteor.users.findOne(...) is undefined

I want to have a user profile so upon registration, the profile field is created using Reactjs:

Registration component (Client):

Accounts.createUser({
 ...
 profile: [] //later you'll see why for this.
});

Server:

// We us this to add more fields to a user registration:
Accounts.onCreateUser(function(options, user) {
  user['regtype'] = options.regtype,
  user['profile'] = options.profile
  return user
});

Meteor.publish(null, function () {
  if (this.userId) {
    return Meteor.users.find({_id: this.userId},
                             {fields: {'regtype': 1, 'profile': 1}});
  } else {
    this.ready();
  }
});

The error comes from my Profile.jsx:

...
mixins: [ReactMeteorData],
  getMeteorData() {
    return {
      currentUser: Meteor.user(),
    };
  },
  getInitialState(){
    profile = Meteor.users.findOne().profile; // the error is here
    return{name: profile.name}
  }
...

Strangely if I console.log(Meteor.user.find()) it shows as undefined. But works great in Chrome only. I have not tried MS Edge.

Upvotes: 1

Views: 100

Answers (2)

Sylar
Sylar

Reputation: 12072

Here is my answer to this. I hope others find this very useful. Basically I wish to bind the input fields with user's data (profile fields). Upon user registration, I add a profile field and set it to an empty array: profile: []

I need not to use getInitialState() but instead use getMeteorData(). With same code, I need to move the component's render in a function and render that:

...

renderedComponent(){
 let instance = this;
 render(<div><input value={instance.data.currentUser.profile.name} /></div>)
},

// we now wait until currentUser is loaded before calling the render function
render(){
 return(<div>{this.data.currentUser? this.renderedComponent() : <p>Loading...</p>}</div>)
}

...

To sum up, nothing is wrong with firefox or any part of my code. When working with Meteor and React, this is the best approach.

Upvotes: 0

tkay
tkay

Reputation: 1726

According to meteor docs

The basic Accounts system is in the accounts-base package, but applications typically include this automatically by adding one of the login provider packages: accounts-password, accounts-facebook, accounts-github, accounts-google, accounts-meetup, accounts-twitter, or accounts-weibo.

So If you have the accounts-base package you'll get the Meteor.user() and Meteor.users() functions. Check your .meteor/packages file for account-base package. Add if it is not listed in there.

Upvotes: 1

Related Questions