Sequelize返回String类型列值作为Buffer数组(MariaDB)

时间:2017-08-16 07:33:03

标签: mariadb sequelize.js

我在我的node.js项目中使用sequelize(http://docs.sequelizejs.com/)作为ORM库。

DBMS是MariaDB(10.1.13-MariaDB-MariaDB Server)

问题是,所有VARCHAR和CHAR类型的列值都作为缓冲区数组返回。 enter image description here

表结构是这样的。 enter image description here

模型文件看起来像这样。 enter image description here

我不知道我做错了什么。

这就是我的期望。

“user_id”:“asdfff”

“email_address”:“asdf@asdf.com”

...

1 个答案:

答案 0 :(得分:0)

我自己解决了这个问题。 我不知道为什么会这样。 如果我连接到MySQL数据库,String返回为String。好像没问题。 但只有当我连接到Maria Database时,String才会以Int Array(int代表字符的ASCII代码)的形式退回

我几天都在挣扎,但我无法弄清楚这一点。 所以我决定编辑一个sequelize的模块文件。

它是" node_modules / sequelize / lib / instance.js"

函数的最后一部分" Instance.prototype.set = function(key,value,options ...", 我添加了几行代码。

    if (!options.raw && ((!Utils.isPrimitive(value) && value !== null) || value !== originalValue)) {
      this._previousDataValues[key] = originalValue;
      this.changed(key, true);
    }

    // HACK : This part for byte array to string of MariaDB
    if (Buffer.isBuffer(value)) {
        value = value.toString('utf8');
    }

    this.dataValues[key] = value;

我不确定这是不是很好的解决方案,但就我而言,到目前为止还没有发生任何问题。