Search code examples
node.jsexpressunexpected-token

Why is it giving an error node expressjs?


There is a problem when i run this code by nodemon it gives me the following error

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)
`});``

Solution

  • The proper syntax for try ... catch is as follows

    try {
       //do something
    } catch (err) {
      console.error(err);
    }
    

    Also you have a bunch of ` at the end, but I think that is just formatting issues in your question. Here is a reference.