Open queston02 opened 4 years ago
There is one more place where variable set in namespace is coming as undefined
. It's in the callbacks defined in the express.router. Please see below for some context.
const express = require('express');
const router = express.Router();
router.post('/dummy', sessionValidator, setJson.bind(dummyProduct, 'getDummyProducts'));
function sessionValidator(req, res, next) {
var namespace = getNamespace('namespace');
console.log(namespace.get('gtid'))
}
I encountered the same issue. The problem is that next callback finishes before the other stuff get called, so I looked at the Namespace.prototype.run source and took what I needed from it. Beware though, I'm reusing the same namespace without destroying it which means that there's a small chance for a race condition, in which a request overwrites the 'req' value before the previous request is done with it (for me it wasn't a concern):
const CLS = require('continuation-local-storage');
function cls(req, res, next) {
let namespace = CLS.getNamespace('api') || CLS.createNamespace('api');
let context = namespace.createContext();
namespace.enter(context);
try{
namespace.set('req', req);
namespace.set('res', res);
return next();
} catch (e) {
console.log('CLS ERR: ' + e);
}
}
...
app.use('/api/*', cls);
We are creating the namespace and setting a variable called
gtid
in one of the middle ware and then trying to read that value for logging. We are getting the values in most of the places but some places it'sundefined
. One of the place is in the callback of the socket close event. Below is the code to provide some context.