How to define global variables in node.js
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".
javascript node.js heroku
add a comment |
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".
javascript node.js heroku
5
Please post your code as text and not images.
– JohnnyHK
Nov 14 '18 at 20:49
add a comment |
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".
javascript node.js heroku
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".
javascript node.js heroku
javascript node.js heroku
edited Nov 15 '18 at 13:43
Martin Sanner
asked Nov 14 '18 at 20:47
Martin SannerMartin Sanner
11
11
5
Please post your code as text and not images.
– JohnnyHK
Nov 14 '18 at 20:49
add a comment |
5
Please post your code as text and not images.
– JohnnyHK
Nov 14 '18 at 20:49
5
5
Please post your code as text and not images.
– JohnnyHK
Nov 14 '18 at 20:49
Please post your code as text and not images.
– JohnnyHK
Nov 14 '18 at 20:49
add a comment |
3 Answers
3
active
oldest
votes
Create in global object
Ex: global.nameVar = 'value'
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.
– Martin Sanner
Nov 15 '18 at 13:35
add a comment |
Try to export your variable like that:
module.exports = 'value' // or object or function or whatever you want
and you can call it again.
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.
– Martin Sanner
Nov 16 '18 at 15:24
add a 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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308476%2fhow-to-define-global-variables-in-node-js%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Create in global object
Ex: global.nameVar = 'value'
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.
– Martin Sanner
Nov 15 '18 at 13:35
add a comment |
Create in global object
Ex: global.nameVar = 'value'
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.
– Martin Sanner
Nov 15 '18 at 13:35
add a comment |
Create in global object
Ex: global.nameVar = 'value'
Create in global object
Ex: global.nameVar = 'value'
answered Nov 14 '18 at 20:48
Henrique VianaHenrique Viana
1997
1997
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.
– Martin Sanner
Nov 15 '18 at 13:35
add a 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.
– Martin Sanner
Nov 15 '18 at 13:35
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.
– Martin Sanner
Nov 15 '18 at 13:35
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.
– Martin Sanner
Nov 15 '18 at 13:35
add a comment |
Try to export your variable like that:
module.exports = 'value' // or object or function or whatever you want
and you can call it again.
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.
– Martin Sanner
Nov 16 '18 at 15:24
add a comment |
Try to export your variable like that:
module.exports = 'value' // or object or function or whatever you want
and you can call it again.
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.
– Martin Sanner
Nov 16 '18 at 15:24
add a comment |
Try to export your variable like that:
module.exports = 'value' // or object or function or whatever you want
and you can call it again.
Try to export your variable like that:
module.exports = 'value' // or object or function or whatever you want
and you can call it again.
answered Nov 15 '18 at 14:33
Hedi LahouarHedi Lahouar
412
412
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.
– Martin Sanner
Nov 16 '18 at 15:24
add a 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.
– Martin Sanner
Nov 16 '18 at 15:24
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.
– Martin Sanner
Nov 16 '18 at 15:24
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.
– Martin Sanner
Nov 16 '18 at 15:24
add a 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.
add a 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.
add a 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.
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.
answered Nov 16 '18 at 15:27
Martin SannerMartin Sanner
11
11
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308476%2fhow-to-define-global-variables-in-node-js%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
5
Please post your code as text and not images.
– JohnnyHK
Nov 14 '18 at 20:49