【问题标题】:SyntaxError: Unexpected identifier with bcrypt moduleSyntaxError:bcrypt 模块的意外标识符
【发布时间】:2020-05-13 06:29:13
【问题描述】:

js ,我创建了一个名为 app.js 的文件。 我用 bcrypt 模块数据库编写了一个登录功能 MySQL 。我几乎找不到导致此错误的代码。我在下面有一个错误,如果您找到任何解决方案,请教我。

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...

我检查了括号和括号,但似乎没有错 这是我的整个app.js 文件

//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");
});

【问题讨论】:

    标签: javascript mysql node.js express


    【解决方案1】:

    您是否尝试过更新或/或重新安装您的 bcrypt 版本?

    【讨论】:

      【解决方案2】:

      首先,我自己还在学习 node.js,但这对我有用...

      原始代码给了我SyntaxError: Unexpected identifier 错误:

      function async (err, user) {
      
        //some code
        
        if (await bcrypt.compare(password, user.password)) {
          return done(null, false, { message: 'Incorrect password.' });
       }            
       
       //some code
       
       }

      我是这样工作的:

      function async (err, user) {
      
        //some code
        
        var comparePwd = async () => await bcrypt.compare(password, user.password)
        if (!comparePwd) {
          return done(null, false, { message: 'Incorrect password.' });
        }            
           
        //some code
           
      }

      【讨论】:

        猜你喜欢
        • 2015-09-14
        • 1970-01-01
        • 2017-09-05
        • 2020-07-10
        • 2012-06-19
        • 2014-04-10
        • 2013-10-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多