I've got a parameterized query using Node server side to do operations on a Postgresql database. When I try to update the table, I get following error
error: invalid input syntax for type json. detail: 'Token "order_cbs1l" is invalid.' where: 'JSON data, line 1: order_cbs1l'
I tried to solve this with JSON.stringify(laveid)
but problem is that JSON.stringify()
adds double quotes around the value and so to the logic of the program order_cbs1l !== "order_cbs1l"
. The program cannot find a match if one value has double quotes and the other does not. Is there a solution for this?
table
id(serial) | info(jsonb)
server.js
var contractorInfo = {
"id": cleanerid,
"fname": fname,
"lname": lname,
"avatar":avatar
}
//stringify array
var cleaner = JSON.stringify(contractorInfo);
//id
var laveid = 'order_cbs1l';
//query
var text02 ="UPDATE users SET info = JSONB_SET(info, '{schedule,0,cleaner}', $2) WHERE info->'schedule'->0->>'id'=$1 RETURNING*";
var value02 = [cleaner,laveid];
//pool
[...]
UPDATE
Here I'm adding the row from the database.
json object
{
"dob": "1988-12-11",
"type": "seller",
"email": "johndoe@gmail.com",
"phone": "5553766962",
"avatar": "image.png",
"schedule": [
{
"id": "order_cbs1l",
"pay": "230",
"date": "2022-12-29",
"status": "Available",
"address": "234 Eleventh Street, Mildura Victoria 3500, Australia",
"cleaner": {
"id": "",
"fname": "",
"lname": "",
"avatar": ""
},
"end_time": "10:15",
"start_time": "01:00",
"total_hours": "33300000",
"paymentIntentId": "pi_3KJnrEFzZWeJoxzV1yUdGLQ8"
}
],
"last_name": "doe",
"first_name": "john",
"countrycode": "Canada: +1",
"countryflag": "iti__ca",
"date_created": "2022-11-12T19:44:36.714Z"
}