0

I have the following piece of code in my server/fixtures.js file:

var userId = Accounts.createUser({
  username: "tester",
  email: "a@b.com",
  password: "foobar",
  profile: { name: "Max" }
});
var user = Meteor.users.findOne({_id: userId});
console.log(user.profile.name);

Now when I run meteor it logs undefined. What am I doing wrong?

the-bass
  • 705
  • 1
  • 6
  • 20

3 Answers3

2

I'm pretty sure I've had an Accounts.onCreateUser callback defined somewhere that was responsible for this. My bad!

the-bass
  • 705
  • 1
  • 6
  • 20
-1

I think you need to use Meteors Publications & Subscriptions. Check this link for more info.

Example:

if (Meteor.isServer){
  Meteor.publish('userdata', function() {   
    if(!this.userId) return null;
    return Meteor.users.find( this.userId );
  });
}

if (Meteor.isClient){
    Meteor.subscribe('userdata');
    console.log(Meteor.user());
}
-1

So, the problem is that console.log is not available on the server (where this file runs according to Meteor directory conventions.). If you would like to log to the server console, you can use Meteor._debug() which works the same as console.log. You may also want to consider wrapping your code in a Meteor.startup(function() {}); block so that it runs as soon as the server spins up.

TDmoneybanks
  • 478
  • 1
  • 7
  • 20
  • 1
    console.log is available on the server. You can see the output in the window you are running the meteor server. I answered this question myself, I assume I've had an Accounts.onCreateUser callback defined somewhere. – the-bass Aug 12 '15 at 17:02