nodejs从函数返回结果

时间:2019-07-20 12:28:44

标签: mysql node.js

这是我编写代码的方式

model\user.js

'user strict';
var sql = require('./../db.js');

//User object constructor
var User = function(user){
    this.email = user.email;
    this.name = user.name;
    this.gender = user.gender;
    this.profileImageType = user.profileImageType;
    this.profileImage = user.profileImage;
    this.accountType = user.accountType;
    this.createdAt = new Date();
};
User.getCountByEmail = function (email, result) {
    console.log("entering user.getCountByEmail with")
    console.log(email)
        sql.query("Select COUNT(*) AS userCount from users where email = ? ", email, function (err, res) {             
                if(err) {
                    console.log("error: ", err);
                    return result(err, null);
                }
                else{
                    console.log("user getCountByEmail returns")
                    console.log(res)
                    return result(null, res);
                }
            });   
};

model\userController.js

'use strict';

var User = require('./user.js');

exports.list_all_users = function(req,res){
    User.getAll(function(err,user){
        console.log('controller')
        if (err){
            res.send(err);
        }else{
            console.log('res', user)
            res.send(user)
        }
    });
};
exports.getCountByEmail = function(email,res){
    User.getCountByEmail(email, function(err,user){
      if (err){
          console.log('error', err)
          return res.send(err)
      }else{
          console.log('res', user)
          console.log(user)
          return user;
      }
    });
};

index.js

var userController = require('./../model/userController.js');
      userController.getCountByEmail("miow@email.com",res,function(err,result) {
        if (err){
          console.log("34 err")
          console.log(err)
        }else{
          console.log("imin userCount: " + result)
        }
      });

现在在上面的index.js中,执行了行userController.getCountByEmail,但是我希望结果可以在函数中访问。不幸的是,似乎代码

function(err,result) {
            if (err){
              console.log("34 err")
              console.log(err)
            }else{
              console.log("imin userCount: " + result)
            }

根本没有执行。我只有线

res [ RowDataPacket { userCount: 1 } ]
imin
[ RowDataPacket { userCount: 1 } ]
位于userController.js内的

。那么如何将userController.js的结果发送到index.js

1 个答案:

答案 0 :(得分:0)

function(err,result) {
            if (err){
              console.log("34 err")
              console.log(err)
            }else{
              console.log("imin userCount: " + result)
            }

之所以不会执行,是因为您在模型/ userController中的getCountByEmail中仅传递了两个参数(email,res)。因此,当您在index.js中调用此函数时,您实际上传递了三个参数。第三个是

    function(err,result) {
            if (err){
              console.log("34 err")
              console.log(err)
            }else{
              console.log("imin userCount: " + result)
            }

我会这样做:

model/userController

exports.getCountByEmail = function(email,res){
    User.getCountByEmail(email, function(err,user){
      if (err){
          console.log('error', err)
          return res.send(err)
      }else{
          console.log('res', user)
          console.log(user)
          res.send(user);
      }
    });
};

index.js

userController.getCountByEmail("miow@email.com",res)

但是通常,控制器将参数(req,res,next)作为参数,res的使用类似于return,因为它会将响应返回给服务器。