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

246
Vistas
Pass Node.js Express variable to my static javascript files

How can I pass one Node.js Express variable to my static JavaScript files? I am trying to do a fetch in my client js, but for that I need 2 ID's that I get from my database. Right now I am cheating this behavior by sending the ID's with the .ejs file. This works, but is far from right.

<span id="user_1"><%= user.ID %></span>
<span id="user_2"><%= matchId %></span>

setInterval(function() {
  let test123 = document.querySelector('.chat_history_section_inner').childElementCount;
  let user_1 = document.querySelector('#user_1').innerHTML;
  let user_2 = document.querySelector('#user_2').innerHTML;;
  fetch('/api?s1=' + user_1 + '&r1=' + user_2 + '&s2=' + user_2 + '&r2=' + user_1, {
    method: 'get'
  }).then(function (response) {
    return response.json()
  }).then(function (res) {
    if (res.length > test123) {
      location.reload();
    } else {
      console.log('Your chat is up to date!')
    }
  });
}, 1000);
about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Did you tried to pass it directly to the variable?

let user_1 = "<%= user.ID %>";

If your js file is not rendered by EJS you have to pass it from an ejs rendered file to your function, or you add it as a custom attribute to your html tag and read the attribute from the id - thats similar to your solution.

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