G
G
grunbyer2022-04-21 18:39:31
JavaScript
grunbyer, 2022-04-21 18:39:31

How to display only one account data from MongoDb?

Good afternoon, when studying the database, I encountered that I can not display the data of only one user. That is, registration and login to the account, the user's personal page displays: "Hi, `username`", but along with his `username` appear `username` of other accounts from mongoDB

const isAuth = (req,res,next)=>{
    if(req.session.isAuth){
        next()
    } else{
        res.redirect('/login')
    }
}
app.get('/register', (req,res)=>{
    res.render('register')
})
app.post('/register', async (req,res)=>{
    const {username, email, password} = req.body

    let user = await UserModel.findOne({email})
    if(user){
        return res.redirect('/register')
    }
    const hashPsw = await bcrypt.hash(password, 12)
    user = new UserModel({
        username,
        email,
        password: hashPsw
    })
    await user.save()
    res.redirect(('/login'))
})
app.get('/dashboard', isAuth ,(req,res)=>{
    UserModel.find({}, function (err, user){
       res.render('dashboard',{
           userList: user
       })
    })
})


<h1>Hi,<%userList.find(user =>{ %>
                    <%= user.username %>
                <% })%>!
            </h1>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Swert, 2022-04-21
@swert-tech

app.get('/dashboard', isAuth ,(req,res)=>{
    UserModel.findOne(/*  тут id авторизированного  */, function (err, user){ // UserModel.find({}, function (err, user){
       res.render('dashboard',{
           userList: user
       })
    })
})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question