Currently, I am experimenting with forms-angular ().
In my index.js
file, I have defined a variable called DataFormHandler
. However, I am facing an issue trying to access this variable in my controllers. The setter
app.set("formHandler", DataFormHandler)
does not seem to work as expected.
Below is the code snippet:
index.js
'use strict';
var formsAngular = require('forms-angular'); // requiring formsAngular
var kraken = require('kraken-js'),
app = require('express')(),
options = {
onconfig: function (config, next) {
//any config setup/overrides here
next(null, config);
}
},
port = process.env.PORT || 8000;
// Initializing FormHandler in index.js and setting it to app for use in controllers.
var DataFormHandler = new (formsAngular)(app);
app.set("fh", DataFormHandler);
app.use(kraken(options));
app.listen(port, function (err) {
console.log('[%s] Listening on http://localhost:%d', app.settings.env, port);
});
The assignment app.set("fh", DataFormHandler)
is not working as expected. When attempting to retrieve fh
in a controller, it returns undefined
:
app\controllers\index.js
'use strict';
var UserModel = require('../models/user');
module.exports = function (router) {
var user = new UserModel();
router.get('/', function (req, res) {
var DataFormHandler = req.app.get("fh");
DataFormHandler.addResource('user', UserModel);
console.log("DataFormHandler", DataFormHandler); // undefined
var model = {
hello: "Hello"
}
res.render('index', model);
});
};
Could you please advise on how to correctly retrieve a variable in a controller?