I’m encountering an issue while trying to insert an array of strings into a PostgreSQL database column. The column is defined as follows:
path TEXT[] NOT NULL
here is the schema:
path: { type: 'array', items: { type: 'string' }, notNull: true }
here I send the format of path like this
await updateGlobalChatNotificationOptIn({
variables: {
path: ['chat'],
option: updatedGlobalChatNotificationOptIn,
},
updateNotificationOption(path: [String!]!, option: String!): NotificationUserOption
when I pass the path something like this ['chat']
// If no existing record, create a new one
return NotificationUserOption.query().upsertGraphAndFetch(
{ userId, path, groupId, option },
{ insertMissing: true },
)
},
extend type Mutation {
updateNotificationOption(path: [String!]!, option: String!): NotificationUserOption
}
However, when I try to insert the array using this code, I encounter the following error:
malformed array literal: "["chat"]"
"[" must introduce explicitly-specified array dimensions.
I’ve tried using single brackets ('{chat}'
) and double brackets ([['chat']]
), but neither seems to work.
How can I correctly format the array literal so that I can insert the array into the PostgreSQL column without encountering this error?
2
Answers
To insert the array of strings into a PostgreSQL column of type
TEXT[]
,you need to convert the JavaScript array into a string representation using curly braces and double quotes for each element.
Do the following in Objection.js:
Now
TEXT[]
will receive the data without any "malformed array literal" errors. Objection.js will handle the conversion back to a JavaScript array when querying the data from the database.#Apache-Age #posgresql #graphql #objection.js
I guess you are passing the path as a string, but the database expects it as an array.
Pass the path as an actual JavaScript array, and i hope Objection.js will handle the conversion to the correct format for the PostgreSQL column.
Make changes as follows:
and then Objection.js function:
It should resolve the error you facing..
#Apache-Age #postgresql #graphql