Reputation: 284
So I have an array that has been created in Node.JS by pushing elements to an array, that I'm then trying to set to a PostgreSQL Text[] column. So it's something like this:
var hours = []
for (x of hourStrings) {
hours.push(x) // Here, x is something like {"2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00"},{},{"2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00"}
}
My element that I'm trying to put into a Postgres column looks like this:
{{"2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00"},{},{"2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00"},{"2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00"},{"2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00"},{"2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00"},{"2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00"}}
and the error I'm getting is this:
error: malformed array literal: "{{"2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00"},{},{"2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00"},{"2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00"},{"2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00"},{"2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00"},{"2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00"}}"
I'm querying with this:
const client = await pool.connect()
await client.query(`INSERT INTO tableName (hoursColumn) VALUES($1) ON CONFLICT DO NOTHING`, [hours]);
I don't see where the error is in the object. Not sure how to fix this, any help is much appreciated.
Upvotes: 0
Views: 2728
Reputation: 1399
Problem is in empty element {} you have in text.
Additionally each element of the array should have the same dimension
Upvotes: 1
Reputation: 11
For this instead of a for loop I used foreach and it seemed to work for me! I also changed up hourStrings.
let hourStrings = ["2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00","2022-05-07T12:00:00+01:00 - 2022-05-07T16:00:00+01:00"]
let hours = []
hourStrings.forEach(element => {
hours.push(element)
});
console.log(hours)
Upvotes: 1