简体   繁体   English

函数在Node.js中被多次调用

[英]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 . 该函数是createWallet After calling it I end up with the database having 2-5 wallets with that user id. 在调用它之后,我最终得到了具有该用户ID的2-5个钱包的数据库。

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 LoginController中的函数activateAccount

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 最后是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: 我已经修改了一下你的代码来提出一个想法,它也将解决你的多个wallet创建问题:

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.");

        });
    });
  });
};  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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