Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在nodejs中如何将结果从模型返回到控制器_Node.js_Ajax_Model View Controller - Fatal编程技术网

Node.js 在nodejs中如何将结果从模型返回到控制器

Node.js 在nodejs中如何将结果从模型返回到控制器,node.js,ajax,model-view-controller,Node.js,Ajax,Model View Controller,我第一次在node js上工作。我的nodejs项目是MVC风格的,我正在做Ajax登录请求。但是我没有从模型中获取数据。。。 这是代码 控制器/Auth.js var data = req.body; Auth_model.login(data, function(err,result){ if(!result){ response = { error:true, message : "Username or Passwo

我第一次在node js上工作。我的nodejs项目是MVC风格的,我正在做Ajax登录请求。但是我没有从模型中获取数据。。。 这是代码

控制器/Auth.js

  var data = req.body;
  Auth_model.login(data, function(err,result){
    if(!result){
        response = {
          error:true,
          message : "Username or Password is wrong."
        };
        res.send(JSON.stringify(response));
      }else{
        response = {
          error:false,
          message : "Logged in Successfully."
        };
        // console.log(result);
        res.send(JSON.stringify(response));   
      }
  });
});
  var data = req.body;

   // here you are passing your callback function as second argument 
   // So, you can use it in your login model when you get your response 

  Auth_model.login(data, function(err,result){ 
   ......... 
  }
Model/Auth_Model.js

module.exports.login = function(data, callback){
  var email = data.email;
  var password  = data.password;
  var sql = 'SELECT * FROM `users` WHERE email='+mysql.escape(email);
  db.query(sql, callback,function (err, result, fields) {
    if (result) {
      bcrypt.compare(password,result[0].password, function(err, result) {
            if(result){
              return result;
            }else{
              return err;
            }
      });
    }
  });
}
module.exports.login = function(data, callback){
    .......
            if(result){
              // use your callback function pass error : null and result:result
              callback(null,result);
            }else{
             callback(err,null)
            }
    ......
}

正如我看到的,您在login函数中传递回调函数,但在login函数的函数定义中,您没有调用回调函数并将数据传递给它

你必须这样做

module.exports.login = function(data, callback) {
  var email = data.email;
  var password = data.password;
  var sql = "SELECT * FROM `users` WHERE email=" + mysql.escape(email);
  db.query(sql, callback, function(err, result, fields) {
    if (result) {
      bcrypt.compare(password, result[0].password, function(err, result) {
        if (result) {
          return callback(null, result);
        } else {
          return callback(err, null);
        }
      });
    }
  });
};
控制器/Auth.js

  var data = req.body;
  Auth_model.login(data, function(err,result){
    if(!result){
        response = {
          error:true,
          message : "Username or Password is wrong."
        };
        res.send(JSON.stringify(response));
      }else{
        response = {
          error:false,
          message : "Logged in Successfully."
        };
        // console.log(result);
        res.send(JSON.stringify(response));   
      }
  });
});
  var data = req.body;

   // here you are passing your callback function as second argument 
   // So, you can use it in your login model when you get your response 

  Auth_model.login(data, function(err,result){ 
   ......... 
  }
Model/Auth_Model.js

module.exports.login = function(data, callback){
  var email = data.email;
  var password  = data.password;
  var sql = 'SELECT * FROM `users` WHERE email='+mysql.escape(email);
  db.query(sql, callback,function (err, result, fields) {
    if (result) {
      bcrypt.compare(password,result[0].password, function(err, result) {
            if(result){
              return result;
            }else{
              return err;
            }
      });
    }
  });
}
module.exports.login = function(data, callback){
    .......
            if(result){
              // use your callback function pass error : null and result:result
              callback(null,result);
            }else{
             callback(err,null)
            }
    ......
}
您也可以使用promise代替回调函数,如

module.exports.login = (data) =  new Promise(function(resolve, reject) {
   .......
            if(result){
              // use your callback function pass error : null and result:result
              resolve(result);
            }else{
             reject(err);
            }
    ......
});

// use it like  : 

 login(data).then(result=>console.log(result)).catch(err=>console.log(err))
了解有关回调函数和承诺的更多信息