node-postgres not parsing queries correctly


I am trying to count the number of documents in a table that satisfy a condition.

My code in node:-

const [washingCount, washedCount, dirtyCount] = await Promise.all([
        pool.query("SELECT COUNT(*) FROM clothes WHERE status = 'washing'"),
        pool.query("SELECT COUNT(*) FROM clothes WHERE status = 'washed'"),
        pool.query("SELECT COUNT(*) FROM clothes WHERE status = 'dirty'")

But I am getting the error saying:

error: column "count" does not exist

And when I copy the same query over to PostgreSQL CLI, those output the desired results.

enter image description here

For full code refer:-

Error stack trace:-
enter image description here

What am I missing here?

Thank you.


Why don’t use group by?

select status, count(*) from clothes group by status


tested with a simple script like this and worked

require("dotenv").config({ path: "./.env" });

const Pool = require("pg").Pool;

const pool = new Pool({
  user: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
  host: process.env.DB_HOST,
  port: process.env.DB_PORT,
  database: process.env.DB_NAME,

async function main() {
  const result1 = await pool.query("SELECT now()");

  const result = await pool.query(
    "SELECT status, COUNT(*) FROM clothes group by status"


  .catch((e) => console.error(e));

Edit 2

Found the issue in your repo code.

Route registration order matters, so you have this route.

app.get("/clothes/:cloth_id", async (req, res) => {

before the count one, so express is getting into this route and never reaching the count one.

To solve the issue move the /clothes/count route before the clothes/clothe_id one and that should solve the issue

Answered By – JuanDM

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published