, column <columnName> is of type jsonb, but the expression is of type text []

Have an array as shown below, you need to save it in a JSONB column:

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

I just pass the req.body object (from Express) to be inserted into the DB.

db.one('insert into foodies(FoodieName, FoodPref,country,languagePref)' +
'values(${FoodieName}, $[FoodPref], ${country} ,${languagePref})  RETURNING FoodieId',req.body)

** PG DB through the pg-prom library causes an error:

{ [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

I think his problem with the cos array: function (arr) driver in formatting.js [in pg-prom lib] returns a string and postgres cannot digest it. If the array is nested in any object, then it works smoothly, for example.

"MyPref" : {Same Object array as above}

Here MyPref goes through the column "FoodPref" without any problems.

+4
source share
1 answer

If the array is nested in any object, it works smoothly

, . , JSONB , .

, , . , :

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

query('bla-bla $1', [data])

. , :

query('bla-bla $1', data)

- JSONB - .

0

Source: https://habr.com/ru/post/1660687/


All Articles