首页 > 解决方案 > 猫鼬在连接超时之前不会返回任何挂起的东西

问题描述

我的登录脚本运行良好,但仅用于一条记录,其余的都挂起并且不返回,不太清楚为什么?

它从我的 console.log 但不是用户的 console.log 重新发送电子邮件和密码。还可以确认数据库中确实存在用户电子邮件

下面是我的基本设置。

用户模型

var mongoose = require("mongoose");
var bcrypt = require("bcrypt");
var passwordString = require("../helper/generateStrongRandomString");

var schema = new mongoose.Schema(
  {
    email: {
      type: String,
      required: true,
      unique: true,
      validate: {
        validator: function (v) {
          return /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/.test(
            v
          );
        },
        message: (props) => `${props.value} is not a valid email address`,
      },
    },
    organisation: {
      ref: "organisation",
      type: mongoose.Schema.Types.ObjectId,
      required: true,
    },
    client: {
      ref: "client",
      type: mongoose.Schema.Types.ObjectId,
      required: false,
    },
    forename: {
      type: String,
    },
    surname: {
      type: String,
    },
    password: {
      type: String,
      default: null,
    },
    created: {
      type: Date,
      default: Date.now(),
    },
    updated: {
      type: Date,
      default: null,
    },
    passwordResetString: {
      type: String,
      default: null,
    },
  },
  {
    toJSON: { virtuals: true },
    toObject: { virtuals: true },
  }
);

schema.pre("save", function (next) {
  var user = this;

  if (!user.isNew) {
    user.updated = Date.now();
  }

  if (user.isNew && !user.password) {
    user.passwordResetString = passwordString();
  }

  if (!user.isModified("password")) {
    return next();
  }

  bcrypt
    .hash(user.password, 12)
    .then((hash) => {
      user.password = hash;
      next();
    })
    .catch((err) => console.log(err));
});

schema.pre("save", function (next) {
  var user = this;
  user.updated = Date.now();
  next();
});

function autopopulate(next) {
  this.populate("organisation");
  this.populate("client");
  next();
}

schema.pre("find", autopopulate);
schema.pre("findOne", autopopulate);

var User = new mongoose.model("user", schema);

module.exports = User;

登录路径

const router = require("express").Router();

const passport = require("passport");

const catchErrors = require("../middlewares/catchErrors");

router.post(
  "/login",
  passport.authenticate("local"),
  catchErrors(async (req, res) => {
    console.log(req.body);
    // console.log(req);
  })
);

module.exports = router;

Passport js 本地认证

passport.use(
  new LocalStrategy(
    {
      usernameField: "email",
      passwordField: "password",
    },
    async function (email, password, cb) {
      console.log(email, password);
      return User.findOne({ email })
        .then((user) => {
          console.log(user);
          if (!user || !bcrypt.compareSync(password, user.password)) {
            console.log(26);
            return cb(null, false, {
              message: "incorrect email or password",
            });
          }
          return cb(null, user, {
            message: "Logged in successfully",
          });
        })
        .catch((err) => console.log(err));
    }
  )
);

标签: node.jsmongodbexpressmongoosepassport.js

解决方案


对于其他偶然发现这一点的人,我想通了。我的问题是我的自动填充和上面未显示的组织模型,请参见下面的代码。

function autopopulate(next) {
  this.populate("organisation");
  this.populate("client");
  next();
}

这会将组织加载并填充为用户数据的一部分,但是当我的组织加载到用户并导致无限循环时,我的组织正在做完全相同的事情。

大量注释代码并检查以确认。

注意无限循环。


推荐阅读