1

Hello Stackoverflow community, I'm new to Node.js / express and i need your help because i stuck at some problem.

I'm trying to make a registration process where the Post inputs are saved into a mongodb. Before i wanna validate if the users email adress already exists in the database.

If i try to define a var at the db.users.find() statement the app wont work.

 app.post("/sign-up", function(req, res){

    var validate;

    db.users.find({email : req.body.email}, function(err, users) {
      if( err || !users){
                    validate = true;
      }else{
            validate = false;
            }
    });

    console.log(validate);

    if(validate == true){
            db.users.save({
                           title: req.body.title,
                           firstname: req.body.firstname,
                           surname: req.body.surname,
                           country : req.body.country,
                           email: req.body.email,
                           password: bcrypt.hashSync(req.body.password, 10)
                                       }, 
                           function(err, saved) {
                                              if( err || !saved ) console.log("User not saved");
                                              else console.log("User saved");
                                              res.location("sign-up-success");
                                              // And forward to success page
                                              res.redirect("sign-up-success");
                                            });
             }
 });

how the db.users.find() functions needs to look like that i can make if/else request for the db.users.save() function.

lhadameck
  • 79
  • 1
  • 7

2 Answers2

2

The problem is that the db.users.find is an asynchronous function. I'll suggest the following:

app.post("/sign-up", function(req, res){
  var validateUser = function(callback) {
    db.users.find({email : req.body.email}, function(err, users) {
      if( err || !users){
        callback(true);
      } else {
        callback(false);
      }
    });
  }
  validateUser(function(isValid) {
    if(isValid == true){
      db.users.save({
        title: req.body.title,
        firstname: req.body.firstname,
        surname: req.body.surname,
        country : req.body.country,
        email: req.body.email,
        password: bcrypt.hashSync(req.body.password, 10)
      }, function(err, saved) {
        if( err || !saved ) console.log("User not saved");
        else console.log("User saved");
        res.location("sign-up-success");
        // And forward to success page
        res.redirect("sign-up-success");
      });
    }
  })
 });
Krasimir
  • 13,306
  • 3
  • 40
  • 55
1

It is async problem, you can move some code to your find callback like this:

 app.post("/sign-up", function(req, res){
    var validate;
    db.users.find({email : req.body.email}, function(err, users) {
        if( err || !users){
            validate = true;
        }else{
            validate = false;
        }

        // your code should be here:
        console.log(validate);

        if(validate == true){
        ....
        }

    });


    // This code will print undefined, so you must move to the callback 
    // console.log(validate)
    // because the callback was not call yet
damphat
  • 18,246
  • 8
  • 45
  • 59
  • thanks need to test it later, for better understanding : can you tell me the reason why the code will print undefined if its not in the callback ? – lhadameck Jan 07 '14 at 10:07
  • 1
    because it is asynchronous, `db.users.find()` will not stop to wait for result – damphat Jan 07 '14 at 10:14
  • Thanks , i think that increased my general understanding for node.js. So i need to do it this way on every asynchron function ? – lhadameck Jan 07 '14 at 10:19
  • 1
    Yes if you want to use the result from the callback, In case you have multiple level of nesting functions, let's use `async` package or something equipvalents – damphat Jan 07 '14 at 10:42