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

2
  • 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. Commented Jan 13, 2019 at 13:50
  • Does this answer your question? knex postgres returns strings for numeric/decimal values Commented Jun 16, 2022 at 12:59

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I do not quite understand how to use node-postgres types with the knexjs, could you explain me better?
Thanks friend, i got it .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.