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

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

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 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