【问题标题】:how to return result from model to controller in nodejs如何将结果从模型返回到nodejs中的控制器
【发布时间】:2020-05-07 03:09:50
【问题描述】:

我是第一次使用 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));   
      }
  });
});

模型/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;
            }
      });
    }
  });
}

【问题讨论】:

    标签: node.js ajax model-view-controller


    【解决方案1】:

    我看到你在登录函数中传递了一个回调函数,但在登录函数的函数定义中你没有调用回调并将数据传递给它。

    你必须做这样的事情。

    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);
            }
          });
        }
      });
    };
    

    【讨论】:

      【解决方案2】:

      控制器/Auth.js

        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){ 
         ......... 
        }
      

      模型/Auth_model.js

      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))
      

      详细了解回调函数和承诺。

      【讨论】:

        猜你喜欢
        • 2018-11-15
        • 1970-01-01
        • 1970-01-01
        • 2020-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多