1

I have a problem in KnexJs I'm using with postgreSQL, but every time I make a select the knex answers all the data in the table as a string, which is not the case, since there are fields that are numbers and the knex Pass me like string someone would know how to help me I'm desperate.

Select knex

function getDeadlines(){
    let query = knex(prazo.getTableName())
                    .select(prazo.properties.idDeadline.getDbProperty(), prazo.properties.deadline.getDbProperty())
                    .orderBy(prazo.properties.deadline.getDbProperty(), 'desc')
    return query
}

Response knexjs code

{
    deadlines: [
      {
        cd_prazo_pk_36: "1", //(Is a numeric in database table)
        ds_prazo_36: "A Vista"
      },
      {
        cd_prazo_pk_36: "2",  //(Is a numeric in database table)
        ds_prazo_36: "7 Dias"
      },
      {
        cd_prazo_pk_36: "4", //(Is a numeric in database table)
        ds_prazo_36: "21 Dias"
      },
      {
        cd_prazo_pk_36: "3", //(Is a numeric in database table)
        ds_prazo_36: "14 Dias"
      }
   ]
}

cd_prazo_pk_36 it's not a string, it's a numeric

Connection

    development: {
    client: 'pg',
    connection: {
      host : 'localhost',
      user : 'postgres',
      password : '123',
      database : 'testing',
      charset: 'utf8'
    },
    useNullAsDefault: true
  },

I already thank you

  • Does your underlying Postgres table definition actually define the columns you expect to be numbers as a numeric type? You might want to include the relevant Knex code in your question. – Tim Biegeleisen Jan 13 '19 at 13:50
  • Does this answer your question? [knex postgres returns strings for numeric/decimal values](https://stackoverflow.com/questions/45569216/knex-postgres-returns-strings-for-numeric-decimal-values) – Marius B Jun 16 '22 at 12:59

1 Answers1

3

node-postgres will convert a database type to a JavaScript string if it doesn't have a registered type parser for the database type. Furthermore, you can send any type to the PostgreSQL server as a string and node-postgres will pass it through without modifying it in any way.

https://node-postgres.com/features/types

Knex uses node-postgres database driver for accessing postgresql.

You can use https://github.com/brianc/node-pg-types to override default parsers for each datatype.

Mikael Lepistö
  • 18,909
  • 3
  • 68
  • 70