0

I have a controller

import request from 'request-promise'
import env from 'dotenv'
const dotenv = env.config();

/*==================================
=            getCPEInfo            =
==================================*/

function getCPEInfo(req, res)
{
    var $cpeMac    = req.body.cpeMac;
    return new Promise((resolve, reject) => {
        request({ uri: `${process.env.API_HOST}/vse/ext/vcpe/${$cpeMac}` })
            .then((cpe) => resolve(JSON.parse(cpe).data))
            .catch((error) => reject(error));
    });

}

module.exports = {
    getCPEInfo
};

I want to call this getCPEInfo() on my other files in my project

I tried import

import generalController from './controllers/general.js'

and call it

generalController.getCPEInfo();

I kept getting

[nodemon] restarting due to changes...
[nodemon] starting `babel-node ./index.js`
/Users/bheng/Desktop/express-app/index.js:72
router.post('/getCPEInfo/:cpeMac', generalController.getCPEInfo);
                                   ^

ReferenceError: generalController is not defined
    at Object.<anonymous> (/Users/bheng/Desktop/express-app/index.js:37:36)
    at Module._compile (module.js:643:30)
    at loader (/Users/bheng/Desktop/express-app/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/bheng/Desktop/express-app/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Function.Module.runMain (module.js:684:10)
    at Object.<anonymous> (/Users/bheng/Desktop/express-app/node_modules/babel-cli/lib/_babel-node.js:154:22)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Function.Module.runMain (module.js:684:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3
[nodemon] app crashed - waiting for file changes before starting...

Can someone please give me a hint ?

cyb3rZ
  • 43,853
  • 82
  • 251
  • 430
  • **Unrelated:** I think you need to abstract logic because calling a controller's function directly it's really ugly. – Ele Apr 18 '18 at 20:03
  • Does it change your change your mind if I am trying to access the helper function ? – cyb3rZ Apr 18 '18 at 20:17
  • If you just want to access the helper function: why not in your general.js do `module.exports = getCPEInfo`; in your index.js `import {getCPEInfo} from 'general.js' ` then use babel. If you don't want to use babel, just do `var helper = import('gernal.js'); helper.getCPEInfo(req, res)` https://stackoverflow.com/questions/45854169/how-can-i-use-an-es6-import-in-node?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – РАВИ Apr 18 '18 at 20:20
  • Try `const generalController = require("./controllers/general.js")` instead of the import stuff – Sello Mkantjwa Apr 18 '18 at 20:25
  • Are you trying to mix ES6 modules with CommonJS? Are you running with [`--experimental-modules`](https://nodejs.org/api/esm.html#esm_enabling) flag? – zero298 Apr 18 '18 at 20:53
  • @zero298 how do I know that ? index.js ? – cyb3rZ Apr 18 '18 at 21:32

1 Answers1

1

import generalController from './controllers/general.js' will refer to the default export from the module, you need to refactor your controller :

import request from 'request-promise'
import env from 'dotenv'
const dotenv = env.config();

/*==================================
=            getCPEInfo            =
==================================*/

function getCPEInfo(req, res)
{
 var $cpeMac    = req.body.cpeMac;
 return new Promise((resolve, reject) => {
 request({ uri: `${process.env.API_HOST}/vse/ext/vcpe/${$cpeMac}` })
        .then((cpe) => resolve(JSON.parse(cpe).data))
        .catch((error) => reject(error));
 });

}

var Mycontroller = {
getCPEInfo
};

export default MyController

Take a look here.

Hamza Fatmi
  • 1,127
  • 6
  • 10