1

I am using node and postgresql (and node-postgres).

I am trying to execute an insert statement, but I get the following error:

error: malformed array literal: "2020-01-15T20:09:22.711+02:00"

My code is as follows:

const queryInsert = {
  text: 'INSERT INTO my_table(some_date, last_update) VALUES($1, $2)',
  values: [2020-01-15T20:09:22.711+02:00, new Date()]
}

  client.query(
    queryInsert.text,
    queryInsert.values,
    (err, res)=> {
      done();
      if (err) {
        console.log('Query: '+queryInsert.text, err);
      }
      console.log(res);
    }
  );

Question

Do I need to somehow format the values if they are dates? If so, how? If not, any advise welcome.

Thank you

1

1 Answer 1

2

node-postgres appears to only accept dates as actual Date objects, not as strings. Documentation

const queryInsert = {
  text: 'INSERT INTO my_table(some_date, last_update) VALUES($1, $2)',
  values: [new Date("2020-01-15T20:09:22.711+02:00"), new Date()]
}
Sign up to request clarification or add additional context in comments.

9 Comments

thank you for your reply. I tried using Date.parse(...) as you suggect, but now I get the following error: error: malformed array literal: "1555937640000".
Ack, sorry, wrong method. You want to use new Date("…") there, not Date.parse(). I updated my answer.
If I add new Date('2019-05-04T08:00:00.000+02:00') I get the following error: malformed array literal: "2019-05-04T08:00:00.000+02:00".
This is strange, because even if I do the following, I get the error, which contradicts the documentation: values: [new Date(]), new Date()].
I changed the columns to type timestamp with time zone, and that fixed it. Thank you for your help!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.