Passing data from client to server


#1

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.


#2

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).


#3

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 ?


#4

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.


#5

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