Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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/1/amazon-web-services/12.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
Arrays ,列<;columnName>;类型为jsonb,但表达式类型为text[]_Arrays_Postgresql_Jsonb_Pg Promise - Fatal编程技术网

Arrays ,列<;columnName>;类型为jsonb,但表达式类型为text[]

Arrays ,列<;columnName>;类型为jsonb,但表达式类型为text[],arrays,postgresql,jsonb,pg-promise,Arrays,Postgresql,Jsonb,Pg Promise,具有如下数组,需要保存在JSONB列中: [{"FoodType":"veg","pref":"High"} ,{"FoodType":"sea food","pref":"Medium"} ,{"FoodType":"Chicken","pref":"Low"}] 我只是将insert的req.body对象(从Express)传递给DB db.one('insert into foodies(FoodieName, FoodPref,country,languagePref)' + 'val

具有如下数组,需要保存在JSONB列中:

[{"FoodType":"veg","pref":"High"}
,{"FoodType":"sea food","pref":"Medium"}
,{"FoodType":"Chicken","pref":"Low"}]
我只是将insert的req.body对象(从Express)传递给DB

db.one('insert into foodies(FoodieName, FoodPref,country,languagePref)' +
'values(${FoodieName}, $[FoodPref], ${country} ,${languagePref})  RETURNING FoodieId',req.body)
**PG DB通过PG promise库抛出错误:

{ [error: column "foodpref" is of type jsonb but expression is of type text[]]
name: 'error',
length: 196,
severity: 'ERROR',
code: '42804',
detail: undefined,
hint: 'You will need to rewrite or cast the expression.',
position: '123',
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'parse_target.c',
line: '529',
routine: 'transformAssignedExpr' }
POST /api/member 500 638.510 ms - 57
我认为它的驱动程序问题是cos-array:formatting.js[in pg promise lib]中的函数(arr)返回字符串,postgres无法消化它。如果数组嵌套在任何对象中,则其工作平稳,例如

"MyPref" : {Same Object array as above}
在这里,MyPref在“FoodPref”列中没有任何问题

若数组嵌套在任何对象中,那个么它可以顺利工作

它指出您没有正确地传递格式化数据。不是将JSONB的数据作为数组传递,而是传递一个内部对象数组

如果您将其作为对象属性传入,它将按照您所说的那样工作。要将其作为数组中的参数传递,需要在数组中传递它:

var data = [{"FoodType":"veg","pref":"High"}
,{"FoodType":"sea food","pref":"Medium"}
,{"FoodType":"Chicken","pref":"Low"}]

query('bla-bla $1', [data])
i、 e.您的问题是,您将其作为:

query('bla-bla $1', data)
这会将array-JSONB数据误解为值-参数数组

更新


.

您应该附上您正在执行的确切查询。要在不执行的情况下获取查询,请使用方法。我要求您使用
pgp.as.format
,而不是传递到查询方法中的内容,在生成查询时更新问题。抱歉:(无法获取查询…请使用示例代码和位置帮助生成查询。我尝试了response.log in。一个方法返回与我在questionCall
pgp.as.format(查询,值)中输入的相同查询)
,它返回格式化的查询字符串。那么我是否应该停止直接传递req.body并为每列创建单独的对象??(检查用于插入的查询的更新问题)很抱歉回复太晚…通过将单个对象作为参数而不是req.body来传递解决了这个问题…另外在json.stringify中传递了变量,该变量具有json数组。