I am very new to Meteor and trying to figure out how to check if a user already exists in the database. The username is being submitted via a form and I want the code to wait for a reply before continuing with the code below it.
I thought I had it figured out but I'm receiving an undefined reply even when I know for a fact that a user already exists in the database.
Here's what I have so far:
In browser console, this executes successfully.
Meteor.users.findOne({'$or': [{'username': 'me@example.com'},{'emails.address': 'me@example.com'}]});
In Mongo terminal console, this executes successfully.
db.users.findOne({'$or': [{'username': 'me@example.com'},{'emails.address': 'me@example.com'}]});
example.js:
if (Meteor.isClient) {
    // This code only runs on the client
    Meteor.subscribe("users");
    Template.registration.events({
        "submit #register_form": function (event) {
            // This function is called when the registration form is submitted.
            var form_data = {
                username: event.target.username.value,
                email: event.target.email.value,
                password: event.target.password.value,
                first_name: event.target.first_name.value,
                last_name: event.target.last_name.value,
                website: event.target.website.value
            };
            // Sync call. Wait for reply before executing remaining code below.
            var result = Meteor.call("check_username", form_data.username);
            alert("result = " + result);  // Returns undefined.
            // Execute remaining code if user does not exist.
        }
    });
}
Meteor.methods({
    check_username: function (username) {
        // Check if user exists.
        var user = Meteor.users.findOne({
            '$or': [
                {'username': username},
                {'emails.address': username}
            ]
        });
        return user;
        // Should I do this instead?
        //if (!user) {
        //  throw new Meteor.Error("user exists", "That user already exists.");
        //}
    }
});
if (Meteor.isServer) {
    Meteor.publish("users", function () {
        return Meteor.users.find();
    });
}
 
     
    