查询返回BIT数据类型的嵌套对象。如何获得位值?

时间:2019-03-01 13:53:31

标签: mysql node.js

我有这个查询:

SELECT
    username AS email,
    optin AS optional
    FROM user AS ua
            INNER JOIN user_preferences AS up ON up.user_id = ua.id
    WHERE ua.id = ${id}

我收到了预期的数据,但是看起来像这样:

let queryResult = await MySQLQueryBuilder.getBasicUserData(userId);

返回的queryResult是:

{
    email: 'someuser@email';
    optional: { type: 'Buffer', data: [1] }
}

我希望得到这样的东西:

{
    email: 'someuser@email.com',
    optional: 1
}

请注意,optin列的数据类型为bit

1 个答案:

答案 0 :(得分:1)

您可以在SQL查询中解决此问题,方法是将位类型转换为int类型,例如,将位类型加0:

SELECT
    username AS email,
    optin+0 AS optional
    FROM user AS ua
            INNER JOIN user_preferences AS up ON up.user_id = ua.id
    WHERE ua.id = ${id}

即使在this fiddle中,您也可以看到您描述的行为。