【问题标题】:Call back function is not working node.js回调函数不起作用 node.js
【发布时间】:2018-11-09 07:15:07
【问题描述】:

我正在尝试使用 node.js 构建登录系统,但我陷入了这个回调函数错误我已经设法构建获取登录信息并使用数据库检查它们但是当我验证密码时需要一些时间我需要使用回调函数,但即使我使用了回调函数,它也会给我同样的错误,因为验证或数据库调用需要时间,它在我的情况下执行其他东西 if 条件。 我试图以另一种方式实现这一点,只是尝试以 console.log 顺序执行所有与我的结果相反的操作

这是它的运行顺序 3 2 不明确的 1 但是我需要完全相反地运行这个我应该使用承诺而不是回调吗?

const {ipcMain} = require('electron');
const Password = require("node-php-password");
const connection = require("./connection");

var hash;
var done;
var self = module.exports = {
   getuser_information:function(user_name,pwd,callback){

    connection.query("SELECT * FROM `super_admin` WHERE ad_un = ?", user_name, function(err, result, fildes) {
        if (err) throw err;
        let numbers_retuned = result.length;
        hash = result[0].desk;
        console.log(1);

    });
    callback(hash,self.true_or_not);
},
hashverif:function(hash,true_or_not){
    true_or_not();
    console.log(2);
},
true_or_not:function(){
    console.log(3);
    return 1

}

}

【问题讨论】:

    标签: node.js callback electron


    【解决方案1】:

    评论后更新

    你有两个错误

    1. 当您的回调函数被调用时,您的 get_stored_password 函数不会返回任何内容,这就是 console.log(function_returning_nothing()) 输出 undefined 的原因

    1. 您忘记将done 传递给您的回调函数 get_stored_password的定义callback(done)

      const {
          ipcMain
      } = require('electron');
      const Password = require("node-php-password");
      const connection = require("./connection");
      
      var hash;
      var done;
      var self = module.exports = {
          get_stored_password: function(name, pwd, callback) {
              connection.query("SELECT * FROM `super_admin` WHERE ad_un = ?", name, function(err, result, fildes) {
                  if (err) throw err;
                  let numbers_retuned = result.length;
                  hash = result[0].desk;
      
                  if (numbers_retuned == 1) {
                      var test = pwd;
                      done = Password.verify(test, hash);
      
      
      
                  } else {
                      console.log('no');
                      return 0;
                  }
      
      
                  //you must pass an argument to your callback function
                  // and return done var to get an output when you log this function
                  callback(done);
                  return done;
              });
      
          },
          chek_if_true: function(done) {
              console.log(done);
              if (done) {
                  return true;
      
              } else {
                  return false;
              }
          }
      }
      

    这就是为什么当您将done 登录到控制台时,它是undefined

    【讨论】:

    • 您在添加它返回 true 之后是正确的,但它来自 chek_if_true 内的控制台日志,但是当我从 main.js 调用该函数时,我从 main.js 调用此函数,它返回一个未定义的:- (
    • console.log(login.get_stored_password('Thaali_Super_Admin',"password123",login.chek_if_true)); 这是来自 main.js 的调用,对于这个调用,它返回一个未定义的
    • 你的get_stored_password函数在你的回调函数被调用时什么都不返回,这就是为什么console.log(function_returning_nothing())输出undefined
    • 抱歉问了,但是我应该把返回放在 get_stored_pa​​ssword 的哪里,我有点困惑,请帮助我尝试过这个,但也不起作用`var re = callback(done);控制台.log(重新);如果(重新){返回1; }`
    • 非常感谢您的快速回复仍然是在完成我正在尝试的其他函数之前调用其他函数的相同错误如果返回值为 1 /true 那么用户是有效用户如果不是用户无效。我会重新检查我的编码......
    猜你喜欢
    • 2019-03-30
    • 2010-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-16
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多