簡體   English   中英

貓鼬直到連接超時才返回任何掛起的東西

[英]Mongoose not returning anything hanging until connection timesout

我的登錄腳本工作正常,但只有一條記錄,其余所有記錄掛起並且不返回,不太確定為什么?

它從我的 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));
    }
  )
);

對於偶然發現此問題的其他人,我想通了。 我的問題是我的自動填充和上面未顯示的組織模型,請參閱下面的代碼。

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

這會將組織作為用戶數據的一部分加載和填充,但是我的組織在將其加載到用戶上時正在做完全相同的事情並導致無限循環。

大量注釋掉代碼並檢查以確認。

注意無限循環。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM