调试项目代码。为什么未定义此功能?

时间:2020-03-16 16:14:23

标签: javascript node.js express

所以我得到一个user.save不是一个函数,而且我似乎不明白为什么它说这不是一个函数,所以我添加了一个后备错误,但仍然无法解决。只是想知道你们中有些人认为问题可能是什么?

users.js:

  router.post("/login", async function(req, res, next) {
  const { email, password } = req.body;

  const user = new UserModel(null, null, email, password);
  const loginResponse = await user.userLogin();
  // console.log('login response is', loginResponse);
  if (!!loginResponse.isValid) {
    req.session.is_logged_in = loginResponse.isValid;
    req.session.user_id = loginResponse.id;
    req.session.name = loginResponse.name;
    res.redirect("/");
  } else {
    res.sendStatus(403);
  }
});


      router.post("/register", async (req, res) => {
      const { name, email } = req.body;
      const salt = bcrypt.genSaltSync(10);
      const hash = bcrypt.hashSync(req.body.password, salt);

      const user = new UserModel(null, name, email, hash);
      user.save().then(() => {
        res.redirect("/users/login");
      });
    });

userModel只是为了防止它出现在模型中: const db = require(“ ./ conn”);

class User {
  constructor(id, job_id, bookmark_id, name, email, password) {
    this.id = id;
    this.job_id;
    this.bookmark_id;
    this.name = name;
    this.email = email;
    this.password = password;
  }
  async addUser() {
    try {
      const response = await db.one(
        "INSERT INTO applicants (name, email, password) VALUES ($1, $2, $3) RETURNING id;",
        [this.name, this.email, this.password]
      );
      return response;
    } catch (error) {
      console.error("ERROR", error);
      return error;
    }
  }

  async loginUser() {
    try {
      const response = await db.one(
        `SELECT id, name, password FROM applicants WHERE email = $1;`,
        [this.email]
      );
      console.log("response is", response);
      const isValid = this.checkpassword(response.password);
      if (!!isValid) {
        const { id, name } = response;
        return { isValid, id, name };
      } else {
        return { isValid };
      }
    } catch (error) {
      console.error("ERROR", error);
      return error;
    }
  }
}
module.exports = User;

0 个答案:

没有答案