Skip to content

Context lost after mysql query #15

@anubhav756

Description

@anubhav756

I'm trying to get http context after a mysql query in express app. The context seems to be lost after executing a query:

const app = require('express')();
const httpContext = require('express-http-context');
const connection = require('mysql').createConnection({ ... });

connection.connect();

app.use(httpContext.middleware);

app.get('/', (req, res) => {
    httpContext.set('foo', 'bar');

    console.log(httpContext.get('foo'));      // prints 'bar'

    connection.query('SELECT * FROM some.table', (err, result, fields) => {
        if (err) throw err;

        console.log(httpContext.get('foo'));  // prints undefined!
        ...
    });
});
...

Please help

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions