0

I want to reuse findemail function

    function findemail(val){
        user.findOne({ email : val }, function (err, person) {
         if(err) throw err;
         if(person == null){
           console.log(false+"1");
           return false;
         }else{
           console.log(true+"1");
           return true;
         }
       });
     }

   router.post('/avail', function(req, res, next){
        \\somecodes
        var email = \\something
        console.log(findemail(email));
        res.send(findemail(email));
   });

output in cmd:

   undefined
   true1

I am a beginner. Can anyone help me?

findone is a mongoose method

Alexis King
  • 43,109
  • 15
  • 131
  • 205
liza
  • 3,687
  • 3
  • 16
  • 18
  • You can't return a value directly from an asynchronous operation like `.findOne()`. – jfriend00 Oct 27 '15 at 07:22
  • @jfriend00 this is not duplicate of that question... and that question does not solve my problem... if u r unable to answer then just ignore my question – liza Oct 27 '15 at 07:29
  • my question is why it is returning an undefined value – liza Oct 27 '15 at 07:30
  • It does too solve your problem. This question is one of the most common node.js questions asked here. You CANNOT directly return a value that is obtained asynchronously. You have to use callbacks or promises JUST like the dup answer describes. Read the part of the answer that has a title `Solution(s)`. That shows you how to solve this problem. Duplicates here on SO are to avoid reposting the same answers over and over again, particularly when a good answer is long like this one: http://stackoverflow.com/a/14220323/816620. Creating a function is two lines of code. Solve your async issue. – jfriend00 Oct 27 '15 at 07:41
  • First you have to realize that your `findemail()` function does not work at all. Fix that first. It only returns `undefined` because the result is obtained asynchronously. If you stubbornly don't want to learn how to program asynchronously in node.js (which is all described in that dup), then there is nothing else we can do you help you here. That is step 1 to solving your problem and is 99% of your issue. – jfriend00 Oct 27 '15 at 07:43
  • @jfriend00 this is server side code. not javascript ajax – liza Oct 27 '15 at 07:45
  • It does not matter. The async issue is exactly the same. Use callbacks or promises. No difference server-side or client-side. – jfriend00 Oct 27 '15 at 07:45
  • 1
    Another similar answer here: [How to return value from an asynchronous callback function?](http://stackoverflow.com/questions/6847697/how-to-return-value-from-an-asynchronous-callback-function). – jfriend00 Oct 27 '15 at 07:47
  • findemail is working... output for findemail is true1... – liza Oct 27 '15 at 07:49
  • No, it isn't fully working. Try `console.log(findemail(someemail))`. You will get `undefined`. It's not returning anything and that's why it doesn't work in your `router.post()` use of it. It may work internally inside the function, but the structure is wrong to return a value. That's what those other answers are offering to teach you. – jfriend00 Oct 27 '15 at 07:50
  • then how to return a value in node.js? – liza Oct 27 '15 at 07:52
  • 1
    FYI this has nothing uniquely to do with node.js. This is ***"How to handle a return value from an asynchronous operation in Javascript?"*** and it applies to any Javascript environment equally, browser, node.js, Rhino, etc.. – jfriend00 Oct 27 '15 at 08:11

1 Answers1

2

OK, I'd suggest you read the other referenced issues to understand the asynchronous issue better. Here's a solution with your code:

  function findemail(val, fn){
        user.findOne({ email : val }, function (err, person) {
            if (err) return fn(false);
            fn(person != null);
       });
   }

   router.post('/avail', function(req, res, next){
        \\somecodes
        var email = \\something
        findemail(email, function(result) {
            res.send(result);
        });
   });

For more details on returning values from an async operation see these references:

How do I return the response from an asynchronous call?

Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference

Node.JS How to set a variable outside the current scope

Order of execution issue javascript

async.js and series issue

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979