猫鼬在连接超时之前不返回任何挂起的东西
Mongoose not returning anything hanging until connection timesout
我的登录脚本工作正常,但只有一条记录,其余的都挂了,不 return 不太清楚为什么?
它从我的 console.log 但不是用户的 console.log 返回电子邮件和密码。也可以确认用户电子邮件确实存在于 db
以下是我的基本设置。
用户模型
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();
}
这会将组织作为用户数据的一部分进行加载和填充,但是当我的组织加载到用户身上并导致无限循环时,我的组织正在做完全相同的事情。
对代码进行大量注释并检查确认。
注意无限循环。
我的登录脚本工作正常,但只有一条记录,其余的都挂了,不 return 不太清楚为什么?
它从我的 console.log 但不是用户的 console.log 返回电子邮件和密码。也可以确认用户电子邮件确实存在于 db
以下是我的基本设置。
用户模型
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();
}
这会将组织作为用户数据的一部分进行加载和填充,但是当我的组织加载到用户身上并导致无限循环时,我的组织正在做完全相同的事情。
对代码进行大量注释并检查确认。
注意无限循环。