-1

I have create controller and model using nodejs. When I try to access model function inside the controller. Its return undefined. Here is my sample code

userController.js file

    var User = require('../models/user');
    // Display list of all Authors.
    exports.user_list = function(req, res) {
    var allUsers = User.allUser();
      console.log(allUsers); //It return undefined
      res.render("../views/users/index", {title : 'Users Listing'});
   };

model/user.js

  var config = require('./../config');
  var User = {
    allUser: function () { 
         config.query("Select * from customer ", function (err, results, fields) {
          return results;
         });
     }
   };
   module.exports = User

index.js file

    //Basic routing.
    var express = require( 'express' );
    var app = express();
    var config = require('./config');
    //Creating Router() object
    var router = express.Router();
    var user_controller = require('./controller/userController.js');
    router.get('/users', user_controller.user_list);
    //router.get('/users/:id/update', user_controller.user_detail);
    app.set('view engine', 'ejs');
    app.use("/",router);
     /* app.use("*",function(req,res){
      res.sendFile(__dirname + "/public/404.html");
     }); */
     // Listen to this Port
    app.listen(3000,function(){
       console.log("Live at Port 3000");
     });
    module.exports = router;
Sudhir
  • 835
  • 11
  • 31
  • 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) – ponury-kostek Jun 15 '18 at 12:44

2 Answers2

0

Because you're not passing a callback argument to the User.allUser() call in userController.js

Try calling it like this:

var allUsers = User.allUser(console.log);

It should print out your results.

Leo
  • 741
  • 6
  • 14
  • I have used it will print out result but allUsers is also undefined – Sudhir Jun 16 '18 at 11:01
  • Ok, so now you need to replace `console.log` by a function that returns your results if you need to do something before returning your data, or just get rid of the callback variable all together and replace the line `return callback(results)` by `return results`. – Leo Jun 16 '18 at 12:47
  • I have changed the return type but already getting same error. Please check – Sudhir Jun 18 '18 at 04:13
0

I think your naming is just off.

In userController.js file: var User = require('../models/user');

However your file path is: model/user.js

Also i'm assuming based on the code userController.js is located in the controller directory.

yourproject
|-- controller
|   `-- userController.js
|-- model
|   `-- user.js      
|-- config.js
|-- index.js
`-- package.json
Woodsy
  • 3,177
  • 2
  • 26
  • 50
  • When I change the path it will generate error Cannot find module – Sudhir Jun 16 '18 at 10:08
  • What is the solution – Sudhir Jun 16 '18 at 10:59
  • @alnitak can you please answer this question – Sudhir Jul 04 '18 at 11:36
  • I'm not sure what to tell you. I copied your code, correct the path to be `var User = require('../model/user');` and was able to run just fine (had to comment out the query stuff but otherwise it works). If you are getting a Cannot find module error you need to verify whats causing the error and make sure the paths are correct you should see this in the error stack where its looking for the module. – Woodsy Jul 05 '18 at 16:10