Expressjs: Is he addicted?

Asked

Viewed 29 times

0

Good morning, dear friends, I am with a project in Expressjs where the function is to use a login panel to generate an HTML file with EJS template containing the attributes of the user who logged in, that is, during each login session a new html file with the user name will be generated and so the Express will redirect the user to their respective page. The problem is that whenever I log in with different users in different browsers it always returns the first user page, even if I log in with the other user’s credentials it will always return the first user.

This is the post code

app.post('/login', function (req, res) {
    const usuario = userAtendente.findOne({
        username: `${req.body.username}`,
        password: `${req.body.password}`
    }, function (err, obj) {
        if (obj == null || obj.isOnline == true) {
            res.send('err')
        }
        else {
            // usuario.updateOne({isOnline: true}).exec().then(result=>{console.log})
            console.log(
                `${obj.nome} acabou de fazer login no restaurante ${obj.restaurante}!`
            )

            ejs.renderFile(
                `./views/painel.ejs`, {
                nome: obj.nome,
            }, (err, html) => {
                fs.writeFile(
                    `./public/painel-${obj.username}.html`,
                    html,
                    err => {
                        res.sendFile(
                            `painel-${obj.username}.html`,
                            { root: `${__dirname}/public/` })
                    })

                console.log(obj.username)
                renderiza(obj.nome)
            })
        }Î
    })
})

This is the function that returns the page

function renderiza(nome){
    app.get('/painel.html', function(req,res){ console.log(nome) })
}

NOTE: I have already checked via console.log the obj.name elements and they return the correct names of the users! It seems that only Express is 'storing' the data of the first user and reusing in other logins. I appreciate if anyone can help me, any suggestion will be welcome!

  • app.get('/painel.html', function(req,res){ console.log(nome) }) -> This ain’t no state guard?

  • I don’t quite understand your code, but I think you missed something like app.get('/painel-${nome}.html' in that capacity renderiza.

  • You gave me a light, buddy. I ended up removing all that structure from res.sendFile and renderiza(), replaced by a simple res.send(html), instead of creating a file and filling the html server, it is easier to send the whole page to be rendered by the client. Thank you so much for your attention!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.