Sequelize converted float to int NodeJS for MySQL DB while read

Viewed 23

I have the following data in MySQL database:

Column: Volume
Value:
3495303.0
3495123.8
3484616.8
3482624.8
3474865.5
3434217.5
3407878.0

I use the following code to read from the database:

let query = {
    where: {
        id: id
    },
    order: [
        ['time', 'DESC']
    ],
}
let result = await markets.findAll(query);

At the following is markets.js

let markets = dbConnection.connection.define(
    'markets', {
        ...,
        volume: Sequelize.FLOAT,
        ...,
    }, {
        timestamps: false
    }
);

module.exports = markets;

However, when column "volume" is read from the database, all the number is converted into int rather than float. I manually executed SQL script generated by Sequelize, and I can see the value of volume has decimals.

However, when the data is loaded into my JSON object "result", the value for volume becomes integer.

I have tried with changing the definition in markets.js from

volume: Sequelize.FLOAT,

to

volume: Sequelize.DOUBLE,

But it does not change a thing. The definition of volume in MySQL is:

volume         float
1 Answers

Alright I figured it out.

The reason being it is doing the wrong thing is the number for volume is overwhelming. There are volume value like "1.0775351E7" which is beyond the capability of FLOAT calculation. I changed the data type in both database and node code to DOUBLE and it is working fine now.

Related