I have the following node.js code:
var path = require('path');
var nodeStack = require('stack-trace');
var controller = (function () {
function controller() {
}
controller.prototype.render = function (res, model, view) {
var stack = nodeStack.get();
var frame = stack[1];
var functionName = frame.getFunctionName().split(/controller\./i);
if (functionName.length < 2) {
functionName = frame.getFunctionName().split(/(controller|\./i);
}
if (!view) {
view = functionName[1];
var dotidx = view.indexOf('.');
if (dotidx > -1) {
view = view.substring(0, dotidx);
}
}
if (!model) {
model = {};
}
var base = '';
if (res.locals.basePath) {
base = res.locals.basePath;
}
var cls = functionName[0];
res.render(path.join(base, cls, view), model);
};
return controller;
})();
module.exports = controller;
The code should allow me to render a view based on the method, the matching file should be found automatically.
It works all fine, just in one case frame.getFunctionName()
returns null.
The code for the subclasses look as follows:
customerController.prototype.add = function (req, res) {
var _this = this;
somepromise.then(function (provider) {
return provider.getCP();
}).then(function (cp) {
controller.prototype.render.call(_this, res, { title: 'add customer', contactpersons: cp });
}, function (err) {
controller.prototype.render.call(_this, res, { title: 'add customer' });
});
};
The one code that doesn't work.
customerController.prototype.details = function (req, res) {
var _this = this;
somepromise.then(function (provider) {
return provider.getCustomerById(req.param('id'));
}).then(function (customer) {
_super.prototype.render.call(_this, res, { title: 'customer details', customer: customer });
}, function (err) {
res.status(404).send(err);
});
};
The code works for all methods except one. Do you have an idea why?
Remarks: The code is compiled from TypeScript