1

I am using node-postgres and attempting to use to_timestamp and receive the error:

ReferenceError: to_timestamp is not defined

Here is my code:

let sDate = format(parseISO(myStartDate),'dd/MM/yyyy')
let sTime = format(parseISO(myStartTime), 'HH:mm:ss')
let sDateTime = `${sDate} ${sTime}`
let eDate = format(parseISO(myEndDate),'dd/MM/yyyy')
let eTime = format(parseISO(MyEndTime), 'HH:mm:ss')
let eDateTime = `${eDate} ${eTime}`

console.log(sDate, sTime, sDateTime)
console.log(eDate, eTime, eDateTime)

data = await pool.query(        
      "INSERT INTO my_table (window_start, window_end ) VALUES($1, $2) RETURNING data_id",
          [ to_timestamp(sDateTime,'YYYY-MM-DD HH24:MI:SS'), to_timestamp(eDateTime,'YYYY-MM-DD HH24:MI:SS') ]
    );

Here is my console output:

25/05/2021 19:56:40 25/05/2021 19:56:40
25/05/2021 19:56:40 25/05/2021 19:56:40

but then errors with ReferenceError: to_timestamp is not defined

Please note that my table columns for both window_start and window_end are of type timestamp

Any ideas please?

1 Answer 1

1

The parameters are not interpolated into the SQL statement, but the to_timestamp function is executed in Javascript, which causes the error.

Use something like

data = await pool.query(        
      "INSERT INTO my_table (window_start, window_end) VALUES (to_timestamp($1, 'YYYY-MM-DD HH24:MI:SS'), to_timestamp($2, 'YYYY-MM-DD HH24:MI:SS') RETURNING data_id",
          [ sDateTime, eDateTime ]
    );
Sign up to request clarification or add additional context in comments.

Comments

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.