no console output with Express?
vlindhol opened this issue ยท 5 comments
I'm a bit stumped on how to get output from pg-monitor
. I initialize pg-promise
and pg-monitor
and then use the db connection in Express.js route handlers. However, there is no console output. An example:
const pgp = require('pg-promise');
const monitor = require('pg-monitor');
const express = require('express');
const db = pgp()({ /* connection parameters */});
monitor.attach({});
const app = express();
app.use(/* routes using db.any etc to query the database */);
app.listen(8080, () => console.log('listening on 8080'));
// outputs "listening on 8080" and then nothing, even if HTTP requests are made and DB activity occurs
Is app.listen
somehow blocking the output from pg-monitor
or am I missing something else? I've been reading and re-reading the docs but I can't find anything obviously wrong ๐
I should add that most of the things above happen in different source files in my real-life code, in case that somehow affects the ability of attach
to... attach. I tried reading the source code of pg-monitor
but can not understand what mechanism is responsible for capturing events from pg-promise
.
Monitor attaches itself to the initialization-options object that you pass into pg-promise, to inject event listeners into it, as supported by pg-promise, and do console output:
const initOptions = {};
const pgp = require('pg-promise')(initOptions);
monitor.attach(initOptions); // attaching to initialization-options object
Whereas what you are doing is attaching monitor to a meaningless empty object.
Ahaa that was the issue! Thank you! It did not occur to me that it was the initOptions
reference that links the two libraries together, I thought it was just a way of passing the same options (none, in my case).
I'll make a pull request to the README.md with a suggestion for a clarification, in case someone else makes the same thinking error :)
Resolved.
In a current version of readme it's still not clear that you must initialize initOptions
as an object and pass it both to pg-promise and pg-monitor constructors. At least I also was using attach({})
approach )
I think a comment like "to make it work you must defince options as an object and pass it to both library constructors" will do the thing