I am trying to refactor an express app, some files are currently around 2k+ lines :(
There is a globalContent object within a file which needs certain values such as version number and array data.
The new globalData object generated via an included function which returns the object (with access to the required data).
version2.js
const { generateGlobalContent } = require('./config/global-content')
var globalContent = generateGlobalContent(req.session, version, transactions)
router.get('/dashboard', function (req, res) {
var manageAccount = globalContent.manageAccount(req);
}
This is the definition of config/global-content.js
module.exports = {
generateGlobalContent: function(req, version, transactions) {
return {
closedAccount: "Your account closed on 28 February 2020.",
manageAccount: function (req) {
if (req.session.account.closed === true) {
return {
heading: "Manage account”,
}
}
}
...
The issue seems to be, there are functions within globalContent that are called from the file which I am requiring the generateGlobalContent function that need to pass the session data.
I am trying to work out how to pass req.session to a function inside the object returned from globalContent:
This is where I am getting the error: TypeError: Cannot read property 'closed' of undefined
Any help would be incredible.
from Passing req.session to an included function in Express
No comments:
Post a Comment