Embed User Name in Url with Iron Router

Okay, I fixed that, now I get this error.

I20150613-06:58:02.385(-7)? Exception while invoking method ‘ATCreateUserServer’ TypeError: Cannot read property ‘userName’ of undefined
I20150613-06:58:02.386(-7)? at [object Object].Meteor.methods.profileInsert (app/lib/collections/profiles.js:11:33)

/collections/profiles.js

  Profiles = new Mongo.Collection('profiles');

  Meteor.methods({
  profileInsert: function(profileAttributes, user) {
  //check attributes etc
  var profile = _.extend(profileAttributes, {
      userId: user._id,       //line 11
      userName: user.profile.userName,
      fullName: user.profile.firstName + " " + user.profile.lastName, 
      submitted: new Date()
  });
  var profiletId = Profile.insert(profile);
  return {
  _id: profileId
   };
}
});

Thanks for all your help so far!

Try this, although I’m not sure what you’re passing when you create a user so it’s difficult to know.

Profiles = new Mongo.Collection('profiles');

  Meteor.methods({
  profileInsert: function(profileAttributes, user) {
  //check attributes etc
  var profile = _.extend(profileAttributes, {
      userId: user._id,       //line 11
      userName: user.username,
      fullName: profileAttributes.firstName + " " + profileAttributes.lastName, 
      submitted: new Date()
  });
  var profiletId = Profile.insert(profile);
  return {
  _id: profileId
   };
}
});
1 Like

u are calling that method with 2 arguments and in the actual method in function you are now accessing them as profileAttributes and user. So inside method you need to access them like profileAttributes.userName

btw you have typo down there, var profiletId and returning profileId, these are different names :smiley:

1 Like

Caught that thanks so much!

Ok that worked! No errors!! Thank you!

How strange, it stores all the user info(Name, Username, etc) in the profile object, but now Meteor.user() isn’t storying anything anymore besides and email, password, and user ID.

I’m assuming on the

Accounts.onCreateUser(function(options, user) {
    user._id = Random.id();
    if(options.profile)
       Meteor.call('profileInsert', options.profile, user);
   return user;
});

I need to somehow pass that data into that function call? How would I do that from the user accounts when the user first signs up? Again, all the data is being passed to the Profiles object, but it stopped being sent to the Meteor.users object

Look into meteor documentation/sources and check how profiles are publicated? :smiley:

The profile is being publicized . It’s when a new user is created the data isn’t being saved from the registration form. It goes to the profile collection, but not the user collection. When I comment out Accounts.onCreateUser() I don’t have any problems.

I need to do something like this is my onCreateUser()?

Accounts.onCreateUser(function(options, user) {
user._id = Random.id();

if(options.profile) {
   Meteor.call('profileInsert', options.profile, user);
}
var newUser = _.extend(options, { 
    firstName: options.profile.firstName,
    lastName: options.profile.lasttName,
    userName: options.profile.usertName
});
return user;
});

Interesting, with a console.log()

Accounts.onCreateUser(function(options, user) {
    user._id = Random.id();
    console.log(options); //here

    if(options.profile) 
    Meteor.call('profileInsert', options.profile, user);

   return user;
});

options is already returning all the data from the user registration form. Why isn’t it saving upon user creation?

Got it to work! Thanks all for your help at patience!! I just needed to assign the options.profile to the user.profile

Accounts.onCreateUser(function(options, user) {
user._id = Random.id();
user.profile = options.profile; //here
console.log(options.profile);

if(options.profile) 
Meteor.call('profileInsert', options.profile, user);

return user;
});
1 Like

Hey, so I tried that code for the route, not working.

I even tried this, since I have a profile object now that holds the userName.

this.route('profile',{
   path:'/:userName',
    data:function(){
     return Meteor.users.findOne({username: this.params.userName});
   }
 });

That’s not working either.

EDIT:

Got it to work! Man I need to hold back before I post something XD

Now I’m trying to make a “go to profile page” link in my navigation.

The route works perfectly

 this.route('profile',{
   path:'/:userName',
    data:function(){
     return Profiles.findOne({userName: this.params.userName});
   }
 });

What doesn’t

   {{pathFor 'profile'}}

work in my my navigation?

so it dont know how to generate link to variable content? :smiley: that is unexpected
it should provide there random.com/asdfgh or random.com/qwerty or ?

I am able to go to the user’s profile page manually by typing in

example.com/userName

But I can’t generate a responsive link for my navigation based on who is logged in.

Oh it was working using

 'click a.profile': function() {
 var p = Meteor.user().profile.userName;
 console.log( p)
 Router.go('/' + p)

}

for a helper. But when I hover over the “Profile” link, my cursor doesn’t change into a hand.

CSS :hover should fix that, or generate it as <a> for example.
in case of href, mby you would need to cancel default action on that click event.

BTW: you did not asked it to generate responsive link for your navigation based on who is logged in. There is no information what that variable represents or that it is linked to current logged user etc…

1 Like

That is a good point! I made at helper and not it acts as a link when i hover over it!

Thank you!!

Template.layout.helpers({
currentLoggedInUser: function() {
  return Meteor.user().profile.userName;
}

});

Give the link href="#" then use e.preventDefault() in your helper. I’m guessing it might not use the hand icon if you have no href defined.

How would I apply e.preventDefault() to my helper?

Template.layout.helpers({
  currentLoggedInUser: function() {
  
  return Meteor.user().profile.userName;
   }
});