SyntaxError: Unexpected identifier with bcrypt module

js , I created a file named app.js .
I coded a login function with bcrypt module database is MySQL . I hardly find which code causes this error . I had a error below , If you find any solution , please teach me .

if (await bcrypt.compare(req.body.password, foundUser.password)) {
          ^^^^^^

SyntaxError: Unexpected identifier
    at Object.compileFunction (vm.js:344:18)
    at wrapSafe (internal/modules/cjs/loader.js:1106:15)
    at Module._compile (internal/modules/cjs/loader.js:1140:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1196:10)
    at Module.load (internal/modules/cjs/loader.js:1040:32)
    at Function.Module._load (internal/modules/cjs/loader.js:929:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47
[nodemon] app crashed - waiting for file changes before starting...

I checked paraenthese and brackets , but it seems no wrong
Here is my whole app.js file

//jshint esversion:6

const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const app = express();
const mysql = require("mysql");
const bcrypt = require("bcrypt");

app.use(express.static("public"));
app.set("view engine", "ejs");
app.use(
  bodyParser.urlencoded({
    extended: true,
  })
);

app.get("/login", (req, res) => {
  res.render("login");
});

app.post("/login", async (req, res) => {
  connection.query(
    "SELECT * FROM user WHERE email=?",
    [req.body.useremail],
    (error, foundUser) => {
      if (foundUser.useremail == null) {
        res.status(400).send("Cannot find user");
      }
      try {
        if (await bcrypt.compare(req.body.password, foundUser.password)) {
          res.render("secrets");
        } else {
          res.send("email or password is wrong");
        }
      } catch {
        res.status(500).send();
      }
    },
  );
});

app.listen(3000, () => {
  console.log("Server is running on port 3000");
});

65 thoughts on “SyntaxError: Unexpected identifier with bcrypt module”

Leave a Comment