Home > Blockchain >  Error: Malformed array literal, trying to pass in an array into TEXT[] column
Error: Malformed array literal, trying to pass in an array into TEXT[] column

Time:05-08

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 don't see where the error is in the object. Not sure how to fix this, any help is much appreciated.

CodePudding user response:

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

Each element of the array should have the same dimension

CodePudding user response:

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)
  • Related