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

385
Views
NodeJS MySQL DATE() returns undefined

I'm trying to get Node to query my database and send the results to the client where it is placed into a <table>, but with the timestamp removed from the date.

I have the following bit of Node querying my database:

pool.query(
    "SELECT DATE(completed), duration, submitted FROM workentries WHERE iin = ?",[iin], 
    function(error, results) {
        if(error) throw error;
        connection.release()
        res.send(results);
    }
)

The client-side JS:

const httpr = new XMLHttpRequest();
httpr.onload = () => {
    let responsej = JSON.parse(httpr.response)
    for(i in responsej) {
        document.getElementById("workentry-table").innerHTML += "<tr><td>"+responsej[i].completed+"</td><td>"+responsej[i].duration+"</td><td>"+responsej[i].submitted+"</td></tr>";
    }
}
httpr.open('GET', '/workentry-list');
httpr.send();

This is the result:

DATE(completed) appears as undefined in the table element

This is how the dates are formatted in the database:

enter image description here

I'm also going to want to reformat the timestamp on the submitted datetime, so it'd be a big help if I can find out how to properly format date/time for this case.

(I'm sorry if this is obvious, but I'm relatively new to Node. I Googled like a madman but couldn't find a solution, which either means this is an uncommon issue or I'm doing something stupid)


EDIT:

  • Fixed undefined by using DATE(completed) AS completed to the query
  • Fixed the date/time format by using toLocaleString() in the client-side
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You need to give an alias to the column in the SELECT list so you can refer to it as responsej[i].completed. Otherwise, you need to use responsej[i]['DATE(completed)']

And to format it as you want, use the DATE_FORMAT() function rather than returning a DATE.

SELECT DATE_FORMAT(completed, '%Y-%m-%d') AS completed, duration, submitted FROM workentries WHERE iin = ?
about 4 years ago · Santiago Trujillo 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!