简体   繁体   中英

convert tinyint field as boolean in result using sequelize raw query node.js

I have problem that when I use a raw query in sequelize which is select query the tinyint fields returned are as integers and not as true / false .

Here is the code:

router.route('/').get(function (req, resp) {
    sequelize.query("select * from territory_device", {model: territoryDevice}).then(result => {
        resp.send(result)
    })
})

And here is the result I recieve:

结果

Although I defined the field is_active as a boolean in the model it gets returned as an integer and I am using MySQL dialect .

The problem is mysql side. Mysql returns 0 or 1 for boolean datatype. You can change the datatype or just handle 0 or 1 in your program

instead of using {raw: true} , you can get Sequelize's wrapper and convert the response using .toJSON() method on the returned response.

then you will get the tinyint parsed as boolean as long as its DataType.BOOLEAN in Sequelize's model object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM