node.js - 猫鼬在连接超时之前不会返回任何挂起的东西
问题描述
我的登录脚本运行良好,但仅用于一条记录,其余的都挂起并且不返回,不太清楚为什么?
它从我的 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();
}
这会将组织加载并填充为用户数据的一部分,但是当我的组织加载到用户并导致无限循环时,我的组织正在做完全相同的事情。
大量注释代码并检查以确认。
注意无限循环。
推荐阅读
- javascript - net::ERR_ABORTED 404(未找到)Laravel 8 HELPPPP
- javascript - Angular ag-grid 出错:“找不到组件类 XXX,您是否忘记配置此组件”
- ruby - 构建的 gemspec 文件(带版本)是做什么用的?
- python - 我的并行处理代码有问题吗?如何使用 multiprocessing.Process 和 multiprocessing.Queue 功能?
- typescript - 打字稿:将动态对象传递给由事件触发的函数
- python - Discord bot 在 heroku 上不起作用:SSL 认证问题
- python - 在 Flask 中向我自己的本地主机发出 POST 请求后出现 WinError 10061
- excel - Excel 高级过滤器显示相同的值
- python - py_call_impl 和 com_error 中的错误:用于 reticulate 的 RPC
- webhooks - 如何在工单的受让人更新/更改时触发 webhook