Passing data from client to server

In client-side, I would like pass the username to server side when a user login. For this, I have a code like this (but it doesn’t work ):

// client-side
Meteor.methods({
     userName : function(){
	 if (Meteor.user())
		return Meteor.user().username;
	 else 
		return "9999";
    }
});

// and in server side I would like take value username of user

var username = Meteor.call('userName'); 

Error: Method ‘userName’ not found [404]
If I was wrong, is there another simple way to do this? Thanks for help.

Are you sure you want to pass the username from the client? The current user is available on the server and cannot be spoofed (unlike any data sent from the client).

Thanks for response, brief in server side I need an information of the user for running my algorithm and Meteor.user().username seems work only in client-side ?

This is not how client-side Meteor methods works. Method declaration is available at client just for optimistic UI, not for passing data around.

If you need user details at server-side, you can always get them via this.userId (considering the user is signed in) inside a method (or publication) and then perform a mongo query for the users details server-side.

1 Like

thanks very much, before I have a bug because I wrote “this.userID” instead of “this.userId” :sweat_smile: