2

For my user registration I have

const express = require ('express');
const userRouter = express.Router ();

userRouter.get ('/', function getUserList (req, res) {
    let User = require ('../models').User;
    User.find ({}, function (err, list) {
        res.json (list);
    });
});

userRouter.post ('/', function createUser (req, res) {
    let User = require ('../models').User;
    if (req.body.username && req.body.password)
        User.create (req.body, function (err, user) {
            res.json (user);
        });
});

... 3 more functions with the same `let User` ...

module.exports = userRouter;

Here, I have to require the module models twice. I tried setting the User variable as a global variable up at the top of the program, like

const express = ..., userRouter = ...
var User = ...

However this User variable is still not accessible inside my callback functions.

Is requiring the User module multiple times the correct way to do this or am I missing something?

edit: Inside the callback functions, the User variable is undefined.

7
  • 3
    no reason it shouldn't be accessible when declared outside the callbacks. What errors occur? Commented Apr 28, 2018 at 11:42
  • I think node has a special global variable Commented Apr 28, 2018 at 11:46
  • 1
    global.User = ... stackabuse.com/using-global-variables-in-node-js Commented Apr 28, 2018 at 11:48
  • 1
    Can you show what is in your models file? Commented Apr 28, 2018 at 11:50
  • @Doug Was going to post that but you mentioned it first so I think you should post that as an answer. Commented Apr 28, 2018 at 11:50

2 Answers 2

1

As @Doug mentioned, you should pass global to the user variable like this:

global.user = ...

This process essentially turns user into a globally accessible variable. You can read more about Node's global here to understand what it does as well as understand what it's implications are too: http://stackabuse.com/using-global-variables-in-node-js/

Sign up to request clarification or add additional context in comments.

4 Comments

No, you should not use a global variable. A module-level constant should be enough.
@Bergi agreed. That is mentioned in the link I provided too. But they also showed which situations may require global variable to be used.
Yes, I'm just saying that this is not one of these situations.
This would make my User variable accessible throughout all modules right? I don't want that, I want it to be accessible in the callbacks of the same module. Which isn't happening because the callbacks are saying that User variable is undefined.
1

To @charloetfl ‘s point if it is just within the file, declaring it outside the callback or on top of the file should do. If we are talking about access across all modules and files within the project then adding it to the global object is the way to go about it in node as @doug and @andrewl mentioned.

2 Comments

Declaring User outside leads to callback functions saying the User variable is undefined.
As using global is also not recommended in node development, I would recommend looking in to creating the User variable within app.locals so it would be accessible in your req object through req.app.locals as described in expressjs.com/en/api.html#app.locals. Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.