Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call one controllers method in another controller in Node

So I have one controller with a method defined as

controller1.js

 router.get('/:id/:field', function(req,res){
   var regex = /action|protocol|ip|port|direction|dip|dport|signature/;
     if (regex.test(req.params.field)){
       get(req,res,function(r){
         var field = req.params.field;
         res.status(200).send(r[field]);
       });
     } else {
         res.status(404).send("Signature Field Does Not Exist");
     }
 });

 function get(req, res, cb){
   MongoClient.connect(url, function(err, db) {
     if (err){
       console.error("Could not connect to database: %s",err);
       res.sendStatus(500);
     } else {
         var _id = req.params.id
         var collection = db.collection("signatures");
         var uniqueID = {"_id":_id};
         var cursor = collection.find(uniqueID); 
         cursor.hasNext(function (err, r) {
           if (err) {console.log(err);}
           else {
             cursor.next(function(err,r) {
               if (r == null){
                 res.status(404).send("Signature not found");
               } else {
                   cb(r);
                   db.close();
               }
             });    
           }    
         });
     }
   });
 }
module.exports = router

This works well in its own class and I can call it from outside via localhost. I want to be able to use both of these in another controller. So in another file

controller2.js

var controller1 = require("./controller1.js");
router.get('/', function(req,res){
  controller1.get(req,res,cb(r){
      res.status(200).send(r);
  });
});

When I attempt to call get in controller2.js I get Error: Route.get() requires callback functions but got a [object Object]. I am sure it is not a database error or connection error of any sort just an error in calling controller1 function from controller2. I've tried changing the header in controller1 to be

router.get = function(req,res,cb){
    ....
});

This makes the get in controller1 unable to call the function.

like image 570
Spothedog1 Avatar asked Jul 05 '16 13:07

Spothedog1


People also ask

Can we call controller method from another controller method?

Yes, you can call a method of another controller. The controller is also a simple class.

Can we call controller inside controller?

In general, you won't use one controller from another one since: Controllers usually return a result of a type intended to be used by the MVC framework.

Can a controller call another controller laravel?

Laravel gives you lots of other other options too i.e. event, jobs, model oberservers etc. If you have two controllers that need to perform the same functionality, then one option is to use a trait which you can then include in both controllers. But you should never need to transfer from one controller to another.

What are controllers in node?

Node Controller (NC) Runs on any machine that hosts VMs, and interacts with both OS and hypervisor to maintain the life cycle of the instances running on each of the host nodes.


1 Answers

To keep your code DRY you can keep all your repeating functions into helper modules.

The structure can be something like this:

controllers  
├── helpers  
    └── index.js  
├── controller1.js   
└── controller2.js  

In the "index.js" helper module you can include your function like this:

exports.yourFunction = function(args){
...
};

And you can call it in the controllers like this:

var helpers = require("./helpers");
...
helpers.yourFunction();

Also, you can find other related answers in this thread:

Javascript - Best way to structure helpers functions in NodeJS

like image 115
Mike Avatar answered Oct 21 '22 13:10

Mike