Search code examples
meteormeteor-accounts

Meteor: How do you use Accounts.findUserByUsername()?


I'm looking at this as the preferred way to find users by case-insensitive username search Accounts.findUserByUsername()

But according to the docs, it's a server method, and I cannot find an example of how to call this method. I tried:

user = Meteor.call('Accounts.findUserByUsername', 'myUserName')

and I got:

Error invoking Method 'Accounts.findUserByUsername': Method 'Accounts.findUserByUsername' not found [404]

Solution

  • You will need to create a server side method and then call it from the client. So the server side method will look something like

    Meteor.methods({
      find_by_username: function(name){
        var user = Accounts.findUserByUsername(name));
        return user;
      }
    });
    

    Then on the client side you will have to invoke the method with a call, and a callback to handle the response from the server

    Meteor.call('find_by_username', 'username', find_by_username_callback)
    
    function find_by_username_callback(error,user){
     console.log(user);
    }