1

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();
    });
}
fire_water
  • 1,380
  • 1
  • 19
  • 33
  • 1
    i like this pattern : https://www.discovermeteor.com/patterns/5828399 I use it for all my Meteor Methods now that need to return a result or error to the client. – looshi Feb 28 '15 at 19:32

1 Answers1

3

This is expected behavior, have a look to my detailed answer here :

Meteor.methods returns undefined

What you need to do is use the asynchronous syntax of the Meteor.call to execute arbitrary code when the result is actually returned from the server.

Meteor.call("check_username", form_data.username,function(error, result){
  if(error){
    console.log(error.reason);
    return;
  }
  alert("result = " + result);  // Returns the result computed on the server
  if(!result){
    // Execute remaining code if user does not exist.
  }
});
Community
  • 1
  • 1
saimeunt
  • 22,666
  • 2
  • 56
  • 61