繁体   English   中英

节点Sequelize mysql转换boolean字段不带<buffer 00></buffer>

[英]Node Sequelize mysql convert boolean field without <Buffer 00>

我用 mysql

我尝试构建一个 rest api。

我定义了一个sequelize model,

module.exports = function(sequelize, DataTypes) {
  return sequelize.define('unseuil', {
    id: {
      autoIncrement: true,
      type: DataTypes.BIGINT,
      allowNull: false,
      primaryKey: true
    },
    actif: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: true
    },
    codeisin: {
      type: DataTypes.STRING(20),
      allowNull: true
    },
    ...

我构建了 2 个查询。

第一的

async function getListeSeuil(codeisin) {
    let liste = await db.unseuil.findAll({ where: { codeisin: codeisin }, order: [
                ['cours', 'ASC']
            ], raw : true });

结果:

[
  {
    id: 2464,
    actif: <Buffer 00>,
    codeisin: 'LU1598757687',
    cours: 10.45,
    datecreation: 2020-07-22T11:57:52.000Z,
    description: 'Resistance'
  },
...

第二

async function getListeSeuil(codeisin) {
    let liste = await db.unseuil.findAll({ where: { codeisin: codeisin }, order: [
                ['cours', 'ASC']
            ] });

结果:

[
  unseuil {
    dataValues: {
      id: 2464,
      actif: false,
      codeisin: 'LU1598757687',
      cours: 10.45,
      datecreation: 2020-07-22T11:57:52.000Z,
      description: 'Resistance'
    },
    _previousDataValues: {
      id: 2464,
      actif: false,
      codeisin: 'LU1598757687',
      cours: 10.45,
      datecreation: 2020-07-22T11:57:52.000Z,
      description: 'Resistance'
    },
...

数据值是正确的。

我解决了这个问题并找到了解决方案。

我想要一个原始值和 actif 为 boolean 的数组。

什么是好方法?

我的第一个解决方案。

async function getListeSeuil2(codeisin) {
    let liste = await db.unseuil.findAll({ where: { codeisin: codeisin }, order: [
                ['cours', 'ASC']
            ], raw : true });

    // correctif d'un bug de reconstruction des booleans.
    var tab = [];
    for (var element of liste)
    {
        tab.push(element.dataValues);
    }
    return tab;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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