question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error: Stream.pipe is not a function

See original GitHub issue

I tried to follow along with the example provided but when i run this code i get an error, stream.pipe is not a function. Im new to pg and nodeJS so Im not exactly sure where/how to fix this issue. This is the code i’ve been trying to run. Side note: Will running pg-query-stream allow me to use the results from the query in other js functions? Thanks, any help/advice is much appreciated.

const {Pool} = require('pg')

const QueryStream = require('pg-query-stream')
const JSONStream = require('JSONStream')

const pool = new Pool({})


const q = 'SELECT * FROM public."HexLogRecord"'

pool.connect()

var stream = pool.query(new QueryStream(q))

stream.pipe(JSONStream.stringify()).pipe(process.stdout)

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
villesaucommented, Dec 30, 2021

Is there a reason why pool.query couldn’t handle the query stream case? I tinkered a bit and found my self basically marrying query stream and connection handling in a way that I think should be handled by the library under the hood, but now in the user space instead.

Now it would look something like this which easily makes the code pretty cluttered and rigid:

  const client = await pool.connect();
  const queryStream = client
    .query(new QueryStream('SELECT * FROM sometable'))
    .pipe(parser);
  queryStream.on('data', handleData);
  queryStream.on('end', () => {
    client.release();
  });
  queryStream.on('error', () => {
    client.release();
  });

while it could as well look like this:

  const queryStream = await pool
    .query(new QueryStream('SELECT * FROM sometable'))
    .pipe(parser);
  queryStream.on('data', handleData);

try{} finally{} does not work with query streams since stream might be still running after the promise will finish.

1reaction
sehropecommented, Feb 12, 2021

You can use the pool but you need to check out the connection as the pool thinks the entire command is complete when the original query function returns. So use client = await pool.connect() and then release the connection after you finish streaming the results.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: stream.pipe is not a function · Issue #90 - GitHub
This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not...
Read more >
Pipe is not a function - Stack Overflow
I have a function that returns a stream and I want to pipe that value to another function then return it in an...
Read more >
Stream | Node.js v19.3.0 Documentation
pipe () method, is to limit the buffering of data to acceptable levels such that sources and destinations of differing speeds will not...
Read more >
ReadableStream.pipeThrough() - Web APIs | MDN
The pipeThrough() method of the ReadableStream interface provides a chainable way of piping the current stream through a transform stream or ...
Read more >
Node.js Stream Introduction. Some definition | by La Javaness IT
Note that Pipe does not propagate errors. There is two way to handle streams errors: First is by adding an on error handler...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found