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

161
Vistas
How to Implement MySQL data into an EJS file to make a Profile Page

I am in the middle of a personal project to practice web development better to understand the ins and outs of web development, but I have run into a brick wall. In this personal project, I am creating a profile page. I have successfully linked the MySQL database to my server js file because I can import data to the table, but I am stuck trying to export the data to an ejs file.

I did the following to export the code, but I cannot even call the data because I cannot have SQL locate any entries from login.

app.get('profile', function (req, res) {
console.log("Inside Get Profile");
connection.query('SELECT * FROM ACCOUNTS WHERE PrimaryEmail = ? ', [req.body.email], function(error, results, fields) {
    if (error){
        console.log("Error");
        console.log(error);
    } else if (results.length > 0) {
        console.log("Data From Get Profile")
        console.log(results);
        res.render('profile', { data: results });
    }})});

So I did the following in an attempt to resolve my issue, which could work, but I am in the matter of pulling the data into the ejs file.

function userProfile(req, res, next){
console.log("Inside userProfile Function");
connection.query('SELECT * FROM ACCOUNTS WHERE PrimaryEmail = ? ', [req.body.email], function(error, results, fields) {
    if (error){
        console.log("Error");
        console.log(error);
        next();
    } else if (results.length > 0) {
        console.log("Data From userProfile Function");
        console.log(results);
        res.render('profile', { data: results });
        next();
    }})};

I would call the function at login after.

app.post('/login', userProfile, passport.authenticate('local', {failureRedirect:'/login-faliure', successRedirect:'/dashboard'}));

Any advice on how to get MySQL data to display certain information for the profile page would suffice!

Thank you for your time!

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Looks like you've implemented the correct code for passing the parameters to EJS (the first code snippet you showed), so there's no reason something like this wouldn't work.

Hello, <%=data.name%>!

This would be because you passed the user data under "data" here:

res.render('profile', { data: user });
about 4 years ago · Juan Pablo Isaza Denunciar

0

I found out that I had two app.get(‘profile’) method and now the page loads with no error after removing the un-needed one. The next thing I had to do was add the code below after “results.length > 0” and my data would start to show.

user={id:results[0].ID, email:results[0].PrimaryEmail, hash:results[0].EncryptHash, password:results[0].EncryptPassword};
    console.log(user);
    res.render('profile', { data: user})
about 4 years ago · Juan Pablo Isaza 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