Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Context lost after mysql query #15

Open
anubhav756 opened this issue Jul 23, 2018 · 8 comments
Open

Context lost after mysql query #15

anubhav756 opened this issue Jul 23, 2018 · 8 comments

Comments

@anubhav756
Copy link

anubhav756 commented Jul 23, 2018

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

@skonves
Copy link
Owner

skonves commented Jul 23, 2018

Thanks for the feedback 👍

What are the current versions of node, npm, and your operating system?

@anubhav756
Copy link
Author

anubhav756 commented Jul 23, 2018

Ubuntu: 16.04 LTS
node: 8.11.3
npm: 5.6.0
express: 4.16.3
express-http-context: 1.0.3
mysql: 2.16.0

Hope that helps 🙂

@Devtronyx
Copy link

Same behaviour for mongoose

osx: 10.14
node: 10.7.0
npm: 6.1.0
express: 4.16.3
express-http-context:1.0.4
mongoose: 5.2.4

@ranjithchev
Copy link

Any answers for this??

@skonves
Copy link
Owner

skonves commented Aug 8, 2018

Thanks @tzira for the info! 😄

I wrote a more detailed status update in #18

@victordidenko
Copy link

victordidenko commented Feb 26, 2019

Just tested with following:

osx: 10.14.3
node: 11.10.0
yarn: 1.13.0

├─┬ @nestjs/core@5.7.3
│ └── express@4.16.3
├── express-http-context@1.2.0
├── mysql@2.16.0
└── typeorm@0.2.14

looks like context isn't lost after mysql queries, works fine for me 🤓
Also tried to run inside docker node:8.12, also works fine.

I'm not sure, why, though. I use async-await everywhere across my code, mysql is used internally by TypeORM.

@zhujun24
Copy link

zhujun24 commented Aug 21, 2019

Same behaviour for mysql

OS: macOS 10.14.6 / CentOS 7
node: 8.9.4
npm: 6.10.3
express: 4.16.4
express-http-context: 1.2.3
mysql: 2.17.1

But new mysql connection everytime will works 😏:

app.get('/', (req, res) => {
  const connection = mysql.createConnection({ xxx })
  const connection.connect()
  connection.query(SQL, (err, result, fields) => {
    httpContext.get('xxx') // it will works
  })
})
``

@zhujun24
Copy link

zhujun24 commented Aug 23, 2019

I fixed it with promisify #946

util.promisify(connection.query).bind(connection)('SELECT * FROM some.table')
  .then((result, fields) => {
    console.log(httpContext.get('foo'));  // prints bar
  })
  .catch(error => {});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants