Can't Search for Usernames with Accounts.FindUserByUsername

Hi everyone,

Would really appreciate some help here. Been banging my head against the table for a couple hours now.

I’m trying to create a simple search function to search for usernames. My goal is to be able to search the usernames and eventually add the selected username to a “teams” collection.

I’ve tried easy-search but had no luck. I’m trying to user Accounts.FindUserByUsername now but I keep getting undefined. I have accounts-password installed, btw.

This is my code:

Server side:

Meteor.methods({
   findUser: function (username) {
      const user = Accounts.findUserByUsername(username)
      return user
   }
})

Client side:

Template.search.helpers(function() {
   Meteor.call('findUser', 'username', findUserCallback)
})
function findUserCallback(error, username) {
   console.log(username)
}
Template.search.events({
   'submit .search'(event) {
      event.preventDefault()
      Meteor.call('findUser', 'username', findUserCallback)
   }
})

I know I’m doing something very wrong but I can’t figure out what. I haven’t found much help online.

Thank you so much – I really appreciate it!

Before everything…, username is optional in the user creation. If not specified, username (login name) is the email. Just to confirm, are you set to create users with a username? If yes, pick a user, what does Meteor.users.find({username: “…” }) returns (server side to skip all possible publication issues.)? Or ‘profile.username’ or wherever the username is saved.

Hi, thanks for the reply! I’m getting username, email and password when a user registers, and having them use the username and password to login.

When I look for db.users.find({}) in terminal I get all the usernames back so I know the username is being stored, so I’m not sure why I can’t seem to search for it…

server side Meteor.users.find()?

Can you put here one document of your user in MongoDB?

Sure! –

{
        "_id" : "xyz45JsBNSpbDiLdY",
        "createdAt" : ISODate("2019-03-02T02:33:53.410Z"),
        "services" : {
                "password" : {
                        "bcrypt" : "$2a$10$b2g5vuRQvVqE5d1jyAWRJe5zmjj.xKXFSxNOPv8/aRcBOxJqC0YF."
                },
                "resume" : {
                        "loginTokens" : [ ]
                }
        },
        "emails" : [
                {
                        "address" : "dave@ca.ca",
                        "verified" : false
                }
        ],
        "userName" : "dave"
}

I don’t know how you are creating a new user, but I think that field would be “username”, with every letters in lowercase. Can you change one document and test?