I'm using zone.js inside my AdonisJs project to create execution context. I have a piece of middleware that creates a zone as follows:
async handle ({ request, response }, next) {
let token = jwtDecode( request.header('Authorization') )
// Create a zone so we have execution context throughout the flow
return Zone.current.fork({
name: Math.random(),
properties: {
username: token.username,
user_id: token.sub
}
}).run( async () => {
// call next to advance the request
await next()
})
}
Inside my controller I can access my zone properties with
let username = Zone.current.get('username')
However, the moment I use the await
command I lose access to the zone properties, all are undefined. For example:
WORKS
let username = Zone.current.get('username') // ALL OK
let order = await Order.find(params.id);
NOT WORK
let order = await Order.find(params.id);
let username = Zone.current.get('username') // UNDEFINED
Can anybody please advise?