How to define global variables in node.js












0















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".










share|improve this question




















  • 5





    Please post your code as text and not images.

    – JohnnyHK
    Nov 14 '18 at 20:49
















0















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".










share|improve this question




















  • 5





    Please post your code as text and not images.

    – JohnnyHK
    Nov 14 '18 at 20:49














0












0








0








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".










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












3 Answers
3






active

oldest

votes


















1














Create in global object



Ex: global.nameVar = 'value'






share|improve this answer
























  • 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



















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.






share|improve this answer
























  • 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



















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.






share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    1














    Create in global object



    Ex: global.nameVar = 'value'






    share|improve this answer
























    • 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
















    1














    Create in global object



    Ex: global.nameVar = 'value'






    share|improve this answer
























    • 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














    1












    1








    1







    Create in global object



    Ex: global.nameVar = 'value'






    share|improve this answer













    Create in global object



    Ex: global.nameVar = 'value'







    share|improve this answer












    share|improve this answer



    share|improve this answer










    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



















    • 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













    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.






    share|improve this answer
























    • 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
















    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.






    share|improve this answer
























    • 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














    0












    0








    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.






    share|improve this answer













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







    share|improve this answer












    share|improve this answer



    share|improve this answer










    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



















    • 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











    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.






    share|improve this answer




























      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.






      share|improve this answer


























        0












        0








        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 15:27









        Martin SannerMartin Sanner

        11




        11






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            這個網誌中的熱門文章

            Academy of Television Arts & Sciences

            L'Équipe

            1995 France bombings