-2

I am running this code but it give me this error. const hashedPassword = await bcrypt.hash(req.body.password, 10) SyntaxError: await is only valid in async functions and the top-level bodies of modules

app.post('/register', (req, res) => { //register/signup
    try{
        const hashedPassword = await bcrypt.hash(req.body.password, 10)
        users.push({
            id: Date.now().toString(),
            name: req.body.name,
            email:req.body,email,
            password: hashedPassword
        })
        res,redirect('/login')
    }catch{
        res.redirect('/register')
    }
    console.log(users)
})`enter code here`
hillol deb
  • 19
  • 3

1 Answers1

0

Just mark the callback function passed to the app.post method as async.


app.post('/register', async (req, res) => { 
    try{
        const hashedPassword = await bcrypt.hash(req.body.password, 10)
        users.push({
            id: Date.now().toString(),
            name: req.body.name,
            email:req.body,email,
            password: hashedPassword
        })
        res,redirect('/login')
    }catch{
        res.redirect('/register')
    }
    console.log(users)
})
Salvino D'sa
  • 4,018
  • 1
  • 7
  • 19