1

I have a module called routeEnforcer.js which contains multiple functions and an object variable called globalUser.

This is the routeEnforcer.js module

I have another module called routeHandler.js which handles all the routes the application takes. I set the routeEnforcer.globalUser to the retrieved user object from the database. When I display the value using console.log, the object is set correctly. When I display the same object in another function, the object is "undefined". I've spent hours trying to figure this out. Can anyone help?

This is the routeHandler.js module showing the required variables.

This is routeHandler.js module showing the post method for login where the routeEnforcer.globalUser is set and displayed successfully.

This is routeHandler.js module showing the get method for the properties view where the routeEnforcer.globalUser variable displays as "undefined".

1
  • 5
    Please post your code as text and not images. Commented Nov 14, 2018 at 20:49

3 Answers 3

4

Create in global object

Ex: global.nameVar = 'value'

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

1 Comment

Thanks, but I tried that and it didn't work. It worked like the example above. The post method would display the value after being set. But, in the get method it was "undefined" again.
0

Try to export your variable like that: module.exports = 'value' // or object or function or whatever you want and you can call it again.

1 Comment

Thanks for your help. I have solved my problem. One thing I forgot to mention was that this app is using the Angular.js framework. My solution was passing the value into the $http.get method within the controller class.
0

Thanks for your help. I have solved my problem. One thing I forgot to mention was that this app is using the Angular.js framework. My solution was passing the value into the $http.get method within the controller class.

Comments

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.