I have nodeJS express setup having the following structure -
-app.js
-routes
---user_router.js
---org_router.js
-controller
---user.js
---organization.js
-model
---userOrganization.js
Now, my app.js
has the routes declarations one after another, and in my both routes their specific controller's get() methods are called
app.js
const express = require('express');
// ---- Some required code ----
const user_router = require('./routes/user_router');
const org_router = require('./routes/org_router');
app.use('/user_router', user_router);
app.use('/org_router', org_router);
// ---- Some required code ----
Routes
user_router.js
const express = require('express');
const userCtrl = require('../../controllers/user');
const router = express.Router();
router.get('/getUser', (req, res) => {
userCtrl.getUsers()
.then((result) => {
res.json(result);
})
.catch((error) => {
res.status(500).send({ message: 'Error' });
});
});
module.exports = router;
org_router.js
const express = require('express');
const orgCtrl = require('../../controllers/organization');
const router = express.Router();
router.get('/getOrg', (req, res) => {
orgCtrl.getOrg()
.then((result) => {
res.json(result);
})
.catch((error) => {
res.status(500).send({ message: 'Error' });
});
});
module.exports = router;
Controllers
user
const userOrganizations = require('../models/userOrganizations.js');
const getUsers = () => new Promise((resolve, reject) => {
userOrganizations.getOrgWiseUser()
.then(resolve)
.catch(reject);
});
module.exports = {
getUsers,
};
Organizations
const userOrganizations = require('../models/userOrganizations.js');
const getOrg = () => new Promise((resolve, reject) => {
userOrganizations.getUserWiseOrg()
.then(resolve)
.catch(reject);
});
module.exports = {
getOrg,
};
Models
userOrganizations
const getUsers = () => new Promise((resolve, reject) => {
resolve(['testUser1', 'testUser2', 'testUser3']);
});
const getOrg = () => new Promise((resolve, reject) => {
resolve(['testOrg1', 'testOrg2', 'testOrg3']);
});
module.exports = {
getUsers,
getOrg,
};
Now, there is plenty of other code resides in my repo including routes, controller, models, and all others, etc.
Now when I call the specific method of org_router
I am facing the error -
userOrganizations.getUserWiseOrg is not a function
If I rewrite controller organization.js
like below then it is working(Just moving model declaration from global to local in function)
const getOrg = () => new Promise((resolve, reject) => {
const userOrganizations = require('../models/userOrganizations.js');
userOrganizations.getUserWiseOrg()
.then(resolve)
.catch(reject);
});
I am not understanding why it is not allowing me to declare the model globally(Why the globally required package is not accessible)
There is one more case if I comment out the require()
statement from the user
Controller then my organization
controller's global require() statement works well and also it is accessible inside the function as well.
My node version is - v12.20.0
What can be the issue? any guess????