-1

Here's how I write my code

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

now in my index.js above, the line userController.getCountByEmail is executed, but I want the result to be accessible inside the function. Unfortunately, it seems the code

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

isn't being executed at all. I only got the line

res [ RowDataPacket { userCount: 1 } ]
imin
[ RowDataPacket { userCount: 1 } ]

which is inside userController.js. So how do I get the result from userController.js sent to index.js ?

imin
  • 4,504
  • 13
  • 56
  • 103
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Luca Kiebel Jul 20 '19 at 12:30

1 Answers1

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

is not executed because in your model/userController getCountByEmail your only pass two parameters (email, res). So when you call this function in your index.js your actually pass three parameters. with the third one being

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

I would do this:

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)

But in general the controller take (req, res, next) as parameter and the res is used like a return because it returns a response to the server.