1
votes

I am currently trying to look up users by email in Angular and Meteor and on my client side code I am using Accounts.findUserByEmail(). On the client I am calling it with Meteor.call() asynchronously. The problem is I can only console.log() or alert() a value back but I can't for example return true or return false. It's not read by my client?

Here is a preview of my server side method:

checkUserByEmail : function (email) {

    this.unblock();

    check(email, Match.Any);
    console.log('Checking to see if ', email, 'is registered')
    var userEmail = Accounts.findUserByEmail(email);
    if(userEmail){
      console.log(userEmail + ' Email account is registered')
      return true
    }
    else{
      console.log(userEmail + ' Email account is not registered' )
      return false
    }
    return userEmail;
  }

Then somewhere on my client:

    $scope.doesUserEmailExist = function(judge){
        Meteor.call('checkUserByEmail', judge.email , function(err, res){
            if(res){
                console.log(judge.email  + ' exists as a registered email!');
                alert('Judge is Valid')
                return true;
            }
            else if(err){
                console.log('Error ',judge.email , 'does not exist!');
                return false;
            }
        })
  }

I am using $scope.doesUserEmailExist to see if a user is registered to my app by entering an email in the input form. When I enter a valid email address I get a console.log value but I can't read any return values? I was hoping that I could use $scope.doesUserEmailExist(email) === true but I haven't had a successful run at it.

3

3 Answers

3
votes

I do not know this Metor but I imagine that Meteor.call is making an asynchronous request to the server, I think you need to do $scope.doesUserEmailExist function return a promise, try this:

$scope.doesUserEmailExist = function(judge){
    var deferred = $q.defer();
    Meteor.call('checkUserByEmail', judge.email , function(err, res){
        if(res){
            console.log(judge.email  + ' exists as a registered email!');
            alert('Judge is Valid')
            deferred.resolve(true);
        }
        else if(err){
            console.log('Error ',judge.email , 'does not exist!');
            deferred.resolve(false);
        }
    })
    return deferred.promise;
}

And to see if it is true:

$scope.doesUserEmailExist(email).then(function(response){
    response === true
})

Don't forget to add $q service as dependence on your controller.

Angular.$q

0
votes

the function

function(judge){

will have finished by the time the function

function(err, res){

is executed. That one is a call back. You are returning false and true to the thing that calls the callback.... not the original function.

You can either call a function that updates whatever you need updating. Or update a reactive variable of some sort ( not quite sure in angular as I do more blaze/react)

0
votes

So I figured out the problem.

Meteor.call() calls the method to the checkUserByEmail which is bound to Meteor's Accounts.findUserByEmail()running from the server. The server call to the client can only console.log a value and not return a value because the client doesn't have fibers.

So the solution is to create a Session variable and bind a value to it and return that value on the client. The solution goes like:

    $scope.doesUserEmailExist = function(judge){
    var exist = Meteor.call('checkUserByEmail', judge.email , function(err, res){
                if(err){
                        console.log(err + ' ' + judge.email , 'does not exist!');
                        Session.set(judge.email, false);
                }
        if(res){
            console.log(res + ' ' + judge.email  + ' exists as a registered email!');
            Session.set(judge.email, res);
        }
                return Session.get(judge.email)
    })
        exist;
        console.log('Session is ' + Session.get(judge.email));
  }

then return the Session variable in your web console with Session.get(variable)

Example : console.log(Session.get('johnsmith@mayflower.com`)) //true

*this applies only to Meteor *