简体   繁体   中英

Function gets called more than once in Node.js

I just noticed one of my functions is called more than once even though I just use it once. The function is createWallet . After calling it I end up with the database having 2-5 wallets with that user id.

The idea is to create just one "wallet" for a user who just activated his account, here's the code:

Route

app.get('/api/activateAccount', function(req, res){
      var mail = req.query.mail;
      var db = require('../controllers/loginController');
      db.activateAccount(mail, function(result){
           res.json(result);
      });
 });

Function activateAccount at LoginController

exports.activateAccount = function(mail, callback){

pool.getConnection(function(err, connection){
  if(err){
    console.log(err);
    callback(true);
    return;
  }

  connection.query("UPDATE usuarios SET activo = 1 WHERE correo = ?", [mail], function(err, results) {
      result = results;

      if(err){
        console.log(err);
      }

        connection.query("SELECT id_usuario AS id FROM usuarios WHERE correo = ?", [mail], function(err, results) {
        result = results;

        if(err){
          console.log(err);
        }
        connection.release();

        createWallet(result[0].id); //this is the only place i call createWallet

        callback("Thank you for confirming your mail you can now Log In with your account.");

        });
    });
  });
};  

And finally createWallet

function createWallet(id_usuario){
var new_wallet = {
  id_usuario: id_usuario,
  coins: 0
};

var string = "NULL";
pool.getConnection(function(err, connection){
  if(err){
  console.log(err);
    callback(true);
    return;
  }

    connection.query('INSERT INTO usuarios_citypoints SET ?', new_wallet, function(err, results){
      connection.release();
      if(err) { 
        console.log(err); 
        callback(true); 
        return; 
      }

      return;
    });
  });
}; 

Why does this happen?

I have modified a bit your code to give an idea, also it will solve your multiple wallet creation problem:

exports.activateAccount = function(mail, callback){

pool.getConnection(function(err, connection){
  if(err){
    console.log(err);
    callback(true);
    return;
  }

    connection.query("SELECT id_usuario AS id FROM usuarios WHERE activo = 0 AND correo = ?", [mail], function(err, user) {
    //connection.query("UPDATE usuarios SET activo = 1 WHERE correo = ?", [mail], function(err, results) {

      if(err){  //check error
        console.log(err);
      }
      else if(user == null) {   //if user not exists
         callback("user not found");
      }
      else if(user[0].activo == 1) {    //if already activated
        callback("already activated");
      }

        connection.query("UPDATE usuarios SET activo = 1 WHERE correo = ?", [mail], function(err, user_activated) {

        if(err){
          console.log(err);
        }
        else if(user_activated.changedRows == 0) { //if no row was updated
            callback("unknown error while updating");
        }
        connection.release();

        createWallet(user[0].id); //this is the only place i call createWallet

        callback("Thank you for confirming your mail you can now Log In with your account.");

        });
    });
  });
};  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM