Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

269
Views
Error: Malformed array literal, trying to pass in an array into TEXT[] column

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.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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)
about 4 years ago · Juan Pablo Isaza Report

0

Problem is in empty element {} you have in text.

Additionally each element of the array should have the same dimension

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!