user4258194
user4258194

Reputation: 71

Why MYSQL is giving Double data type value as string in NodeJs?

I am trying to get a double data type value from Mysql in Nodejs, but that value is coming as String javascipt data type.

I have millions of rows and I want those data to come as javascipt number type

// I am using this package
// const mysql = require("mysql2");

router.get("/prices/:symbol", (req, res) => {
    const q = `SELECT time, price
                FROM price_table
                WHERE symbol='${req.params.symbol}'
                LIMIT 1`
    connection.query(q, (error, rows) => {
        if (error) {
            console.log(error)
        } else {
            
            // Mysql INT data type - time is in UNIX TimeStamp
            console.log(typeof rows[0].time)            // console.log showing "number"
            
            // Mysql Double data type
            console.log(typeof rows[0].price)           // console.log showing "string"

            res.send(rows)
        }
    })
})

Upvotes: 0

Views: 1397

Answers (1)

solognt
solognt

Reputation: 66

According to the documentation:

DECIMAL and NEWDECIMAL types always returned as string unless you pass this config option:

{
  decimalNumbers: true,
}

There is a note that says:

This option could lose precision on the number as Javascript Number is a Float!

Upvotes: 3

Related Questions