在React Web App中使用bcrypt通过无服务器功能散列Mongoose数据库条目

问题描述

我正在尝试使用Bcrypt将条目散列到数据库中。但是,提交后什么也没发生-条目成功提交,但条目没有散列。

此哈希在无服务器功能支持的React Web应用程序中。我的文件结构为:

我的输入模式:

const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const bcrypt = require("bcrypt");

const SubmitDebtSchema = new Schema ({
  firebaseId: String,personalLoan: String,provider: String

SubmitDebtSchema.pre("save",(next) => {
  const user = this;
  bcrypt.hash(SubmitDebtSchema,10,(error,hash) => {
    user.firebaseId = hash;
    user.personalLoan = hash;
    user.provider = hash;
    next();
  });
});

module.exports = mongoose.model('submitdebt',SubmitDebtSchema);

我的“提交”功能

const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const SubmitDebt = require("./submitDebtSchema");
const bcrypt = require("bcrypt");

require("dotenv").config();

const app = express();

app.use(bodyParser.urlencoded({
  extended: true
}));

mongoose.connect(process.env.MONGO_URI);


module.exports = async (req,res) => {

  res.statusCode = 200;
  res.setHeader("Content-Type","application/json");

  await SubmitDebt.create(req.body)
  .then(() => {
    res.send(JSON.stringify({ success: true }));
  })
  .catch((err) => {
    console.log(err);
    res.send(JSON.stringify({ success: false }));
  });

};

我的React前端中的AXIOS发布函数

  onSubmit = async (e) => {

    e.preventDefault();

    await axios.post("/api/submitDebt",{
      firebaseId: this.state.firebaseId,personalLoan: this.state.personalLoanToggled,provider: this.state.provider
    })

    this.props.history.push('/dashboard');
  }

我不明白为什么条目不进行哈希处理-我以前使用bcrypt没问题,并且遵循了将哈希项放入数据库的文档(我认为)。

如果有人有任何指针,将不胜感激!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)