Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

386
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda