读取 JavaScript 中的数据并将其存储在 PostgreSQL 数据库 table 中
Read data in JavaScript and store it in the PostgreSQL Database table
我正在从设备获取十六进制格式的数据,如下所示:
<Buffer 00 cc>
我需要以 TEXT 格式读取相同的内容并将值存储在数据库中。我使用 PostgreSQL 作为数据库来存储数据。
以下是我在将其处理到数据库时收到的错误消息。
error: invalid message format
at Connection.parseE (/var/www/html/project/collection/node_modules/pg/lib/connection.js:614:13)
at Connection.parseMessage (/var/www/html/project/collection/node_modules/pg/lib/connection.js:413:19)
at Socket.<anonymous> (/var/www/html/project/collection/node_modules/pg/lib/connection.js:129:22)
at Socket.emit (events.js:182:13)
请告诉我如何纠正这个问题。
如果您正在处理包含文本数据字节的缓冲区,您可以尝试将缓冲区转换为字符串,然后将其传递给您的数据库调用。考虑这个简单的例子:
const responseBuffer = await someService.getData();
const contentString = responseBuffer.toString('utf8'); // you need to make sure the encoding is correct
await dbService.query(buildQuery(contentString), ...);
我正在从设备获取十六进制格式的数据,如下所示:
<Buffer 00 cc>
我需要以 TEXT 格式读取相同的内容并将值存储在数据库中。我使用 PostgreSQL 作为数据库来存储数据。
以下是我在将其处理到数据库时收到的错误消息。
error: invalid message format
at Connection.parseE (/var/www/html/project/collection/node_modules/pg/lib/connection.js:614:13)
at Connection.parseMessage (/var/www/html/project/collection/node_modules/pg/lib/connection.js:413:19)
at Socket.<anonymous> (/var/www/html/project/collection/node_modules/pg/lib/connection.js:129:22)
at Socket.emit (events.js:182:13)
请告诉我如何纠正这个问题。
如果您正在处理包含文本数据字节的缓冲区,您可以尝试将缓冲区转换为字符串,然后将其传递给您的数据库调用。考虑这个简单的例子:
const responseBuffer = await someService.getData();
const contentString = responseBuffer.toString('utf8'); // you need to make sure the encoding is correct
await dbService.query(buildQuery(contentString), ...);