admin管理员组文章数量:1415653
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".
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 edited Nov 15, 2018 at 13:43 Martin Sanner asked Nov 14, 2018 at 20:47 Martin SannerMartin Sanner 211 silver badge3 bronze badges 1- 5 Please post your code as text and not images. – JohnnyHK Commented Nov 14, 2018 at 20:49
3 Answers
Reset to default 4Create in global object
Ex: global.nameVar = 'value'
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.
本文标签: javascriptHow to define global variables in nodejsStack Overflow
版权声明:本文标题:javascript - How to define global variables in node.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745176773a2646275.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论