Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

266
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda