简体   繁体   English

读取 JavaScript 中的数据并将其存储在 PostgreSQL 数据库表中

[英]Read data in JavaScript and store it in the PostgreSQL Database table

I am getting a data from device in the HEX format like below:我正在从设备获取 HEX 格式的数据,如下所示:

<Buffer 00 cc>

I need to read the same in TEXT format and store the value in Database.我需要以 TEXT 格式读取相同的值并将值存储在数据库中。 I am using PostgreSQL as database to store the data.我使用 PostgreSQL 作为数据库来存储数据。

Below is the error message I am getting while processing it to the Database.以下是我在将其处理到数据库时收到的错误消息。

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)

Please let me how to rectify this.请让我如何纠正这个问题。

If you're dealing with a buffer that contains bytes of text data, you can try to convert the buffer to a string, which you can then pass to your db-call.如果您正在处理包含文本数据字节的缓冲区,您可以尝试将缓冲区转换为字符串,然后您可以将其传递给您的 db-call。 Consider this simple example:考虑这个简单的例子:

const responseBuffer = await someService.getData();
const contentString = responseBuffer.toString('utf8'); // you need to make sure the encoding is correct
await dbService.query(buildQuery(contentString), ...);

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

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