Sequelize NodeJS 服务器抛出“ERR_UNKNOWN_ENCODING”错误

Posted

技术标签:

【中文标题】Sequelize NodeJS 服务器抛出“ERR_UNKNOWN_ENCODING”错误【英文标题】:Sequelize NodeJS server throwing "ERR_UNKNOWN_ENCODING" error 【发布时间】:2021-02-21 07:43:31 【问题描述】:

谁能帮我找出问题所在。我收到此错误:

node:internal/streams/writable:296
       throw new ERR_UNKNOWN_ENCODING(encoding);
       ^

 TypeError [ERR_UNKNOWN_ENCODING]: Unknown encoding: <ref *1> Handshake 
   _events: [Object: null prototype],
   _eventsCount: 1,
   _maxListeners: undefined,
   sequenceNo: 1,
   compressSequenceNo: -1,
   resolve: [Function: bound _authSucceedHandler],
   reject: [Function: bound _authFailHandler],
   sending: false,
   _createSecureContext: [Function: bound _createSecureContext],
   _addCommand: [Function: bound _addCommandEnable],
   getSocket: [Function: _getSocket],
   onPacketReceive: [Function: parseHandshakeInit],
   plugin: [Circular *1],
   [Symbol(kCapture)]: false
 
     at new NodeError (node:internal/errors:259:15)
     at Socket.Writable.write (node:internal/streams/writable:296:13)
     at PacketOutputStream.flushBufferBasic (/var/www/app/node_modules/mariadb/lib/io/packet-output-stream.js:444:17)
     at Object.send (/var/www/app/node_modules/mariadb/lib/cmd/handshake/client-handshake-response.js:118:7)
     at Handshake.parseHandshakeInit (/var/www/app/node_modules/mariadb/lib/cmd/handshake/handshake.js:82:31)
     at PacketInputStream.receivePacketBasic (/var/www/app/node_modules/mariadb/lib/io/packet-input-stream.js:104:9)
     at PacketInputStream.onData (/var/www/app/node_modules/mariadb/lib/io/packet-input-stream.js:169:20)
     at Socket.emit (node:events:327:20)
     at addChunk (node:internal/streams/readable:304:12)
     at readableAddChunk (node:internal/streams/readable:279:9) 
   code: 'ERR_UNKNOWN_ENCODING'
 

我目前不知道出了什么问题。想通了,问题出在这里:

const sequelize = new Sequelize(process.env.DB_DATABASE, process.env.DB_USER, process.env.DB_PASSWORD, 
    host: process.env.DB_HOST,
    port: process.env.DB_PORT,
    dialect: 'mariadb',
);

sequelize.authenticate()
    .then(() => 
        logger.log('info', 'Connected to database')
    )
    .catch((error) => 
        logger.log('error', 'Failed to connect to database!');
        logger.log('error', JSON.stringify(error));
    )

authenticate 函数中。数据库:mariadb:10.5.7-focal (docker)

【问题讨论】:

【参考方案1】:

之前一切正常,因为我使用的是旧版本的 MariaDB,必须运行 npm update 并且问题自行解决。

【讨论】:

您好,我使用 mariadb 作为方言。请问如何设置时区?在方言选项中使用时区不会产生效果。 created_at 和 updated_at 仍然是根据服务器的时区。 @LukeGalea 很乐意回答您的问题,但我自己无法解决此问题,目前没有时间阅读文档。

以上是关于Sequelize NodeJS 服务器抛出“ERR_UNKNOWN_ENCODING”错误的主要内容,如果未能解决你的问题,请参考以下文章

nodejs使用sequelize操作mysql实例

nodejs:sequelize数据库查询的Op方法

如何在 SEQUELIZE (nodeJS) 中创建触发器?

NodeJS/Sequelize/MySQL - 为啥需要 postgres 依赖项?

使用 Sequelize (NodeJS) 而不是 * 指定特定字段

在nodejs sequelize中循环获取异步数据