Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js NodeJS–;在PostgreSQL查询中添加字符串数组_Node.js_Arrays_Postgresql_Sql Insert_Node Postgres - Fatal编程技术网

Node.js NodeJS–;在PostgreSQL查询中添加字符串数组

Node.js NodeJS–;在PostgreSQL查询中添加字符串数组,node.js,arrays,postgresql,sql-insert,node-postgres,Node.js,Arrays,Postgresql,Sql Insert,Node Postgres,我正在尝试编写一个postgres查询(使用node postgres包创建的池在nodejs中执行),该查询将在表中插入新行。此表中的一列类型为text[]。我的代码如下: pool.query('INSERT INTO paragraphs (lines) VALUES (ARRAY[$1]::TEXT[]) RETURNING id', [my_array], (err, results) => { if (err) { rejec

我正在尝试编写一个postgres查询(使用node postgres包创建的池在nodejs中执行),该查询将在表中插入新行。此表中的一列类型为
text[]
。我的代码如下:

pool.query('INSERT INTO paragraphs (lines) VALUES (ARRAY[$1]::TEXT[]) RETURNING id', [my_array], (err, results) => {
            if (err) {
              reject(err)
              return;
            }
            
            resolve(results.rows[0].id)
})
段落
是我表格的名称,
是类型为
text[]
的列的名称<代码>我的数组是字符串列表。我的问题是,插入的不是一个字符串数组,而是一个格式化为数组的单个字符串。e、 十:

{“[\“第一行”、“第二行”、“第三行”]}

我希望它是:

{“第一行”、“第二行”、“第三行”}

我还尝试取出
数组
文本
部分(因此sql看起来像
插入到上面查询中返回id的段落(行)值($1)),但随后我收到了错误:

malformed array literal: "["New First line", "Second line", "Third Line"]"
DETAIL:  "[" must introduce explicitly-specified array dimensions.
通过池执行的查询将字符串列表插入nodejs中的PostgreSQL表的正确方法是什么?

根据:

作为第二个参数传递给query()的参数将使用以下规则转换为原始数据类型:

排列

转换为描述Postgres数组的字符串。每个数组项都使用此处描述的规则进行递归转换

因此:


VALUES($1)
VALUES($1::TEXT[])
应该足够了。

我最初尝试过。事实证明,我试图用作参数的数组实际上只是一个字符串(我使用的是body解析器,出于某种原因,它没有解析我的post body中的数组,并将其保留为字符串)。修复此问题并使用您的方法有效!