Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

298
Vistas
Getting count of rows from Knex

I have a async function that is attempting to return a count of rows in a table "posts"

async getPostCount() {
    return db('posts').count('id');
}

This is used in an API...

router.get(
    '/count',
    async (req, res, next) => {
        try {
            await PostsService.getPostCount()
                .then(result => {
                    res.json({
                        count: result,
                        token: req.query.secret_token
                    })
                    
                })
                .catch((err) => {
                    throw(err);
                });
        }
        catch (err) {
            console.log(err);
            res
                .status(500)
                .json({ success: false, msg: `Something went wrong. ${err}` });
        }
    }
)

However, I get an error and have not been able to find anything on the internet about it.

Something went wrong. error: select * from "posts" where "id" = $1 limit $2 - invalid input syntax for type integer: "count"

What could be going on?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The results from knex.js query builders are arrays. A query could be successful and simply return 0 results.

Also, you can try using an alias for the column directly in the column name (or count() call). Something Like this:

async getPostCount() {
    return db('posts').count('id as CNT').first()
}

....

 await PostsService.getPostCount()
                    .then(result => {
                        res.json({
                            count: result.CNT,
                            token: req.query.secret_token
                        })
                    })
    
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda