在模式方法内部,“this”在猫鼬 4.4.12 中为空 {}
Inside Schema method scopes "this" is empty {} in Mongoose 4.4.12
对象"this"在Schema方法中记录到控制台时是"{}"。这发生在一天前,我一直在阅读教程和其他堆栈溢出问题,但没有找到解决方案。
这是我的模型:
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var constants = require("../config/constants");
var bcrypt = require("bcrypt-nodejs");
var UserSchema = new Schema({
name: String,
email: String,
password: String,
authorization:
{
type: Number,
default: constants.authorization.default
}
});
UserSchema.pre("save", (next) => {
var user = this;
/**
* Only hash the password when it's been modified or if new.
*/
// #####################################################
// ERROR
// if (!query.isModified("password"))
// ^
// TypeError: query.isModified is not a function
//
// user and this == {}
// ####################################################
if (!user.isModified("password"))
{
return next();
}
/**
* hash password
*/
bcrypt.hash(user.password, null, null, (err, hash) => {
if (err)
{
return next(err);
}
user.password = hash;
return next();
});
});
// #####################################################
// ERROR
// user.verifyPassword(req.body.password, match => {
// ^
// TypeError: user.verifyPassword is not a function
//
// this == {}
// ####################################################
UserSchema.methods.verifyPassword = (reqPassword, callback) => {
bcrypt.compare(reqPassword, this.password, (err, match) => {
var e = null;
var m = match;
if (err)
{
e = err;
m = false;
}
return callback(e, m);
});
};
module.exports = mongoose.model("User", UserSchema);
这就是我使用它的方式(我已经标记了中断发生的位置):
//includes express, mongoose, User, constants. this part is ok.
/**
* Authenticates a user post request
*
* @request email string
* @request password string
*
*/
router.post("/", (req, res) => {
/**
* Retrieve the user
*/
User.find(
{
email: req.body.email
},
(err, user) =>
{
/**
* An error occurred
*/
if (err)
{
return res.json({
success: false,
message: "An mongodb error occurred.",
error: err
});
}
/**
* Check for problems with the email or password.
*/
if (!user)
{
return res.json({
success: false,
message: "Email or password was incorrect."
});
}
// ##########################################################
// ERROR
// user.verifyPassword(req.body.password, match => {
// ^
// TypeError: user.verifyPassword is not a function
// ##########################################################
user.verifyPassword(req.body.password, match => {
if (!match)
{
return res.json({
success: false,
message: "Email or password was incorrect."
});
}
/**
* User authenticated!
*/
req.session.user = user;
res.json({
success: true,
message: "Successfully authenticated."
});
});
});
});
router.get("/", (req, res, next) => {
var admin = new User({
name: "admin",
email: "admin@admin.net",
password: "admin",
authorization: constants.authorization.admin
});
// ########################################################
// ERROR
// if (!user.isModified("password"))
// ^
// TypeError: user.isModified is not a function
// #######################################################
admin.save(function(err) {
if (err)
{
console.log(err);
}
console.log('User saved successfully');
res.json({ success: true });
});
});
有谁知道这个问题吗?
该问题可能与您使用 ES6 箭头语法而不是普通函数作为回调有关。ES6 箭头函数更改this
关键字的语义,这可能会影响猫鼬在内部处理回调的方式。
尝试
UserSchema.pre("save", function(next) {
// ...
});
而不是
UserSchema.pre("save", (next) => {
// ...
});
相关文章:
- 多次调用方法后返回相同promise的模式
- 有没有一种方法可以在设计模式下将ng模型或工厂绑定到iframe
- 在模式方法内部,“this”在猫鼬 4.4.12 中为空 {}
- 将变量传递给 Javascript 模块化模式方法
- Django设计模式-在加载时填充客户端JavaScript变量的方法
- Safari限制?使用多种方法来显示多个自定义模式对话框
- 有没有更好的方法可以在ng网格中切换编辑模式
- 如何在字符串上组合JS replace()方法中的3个正则表达式模式
- 是否可以使find()和findOne()方法只返回模式字段
- 任何匹配模式的方法,要么在某个字符之前,要么在某个字符之后
- 它是什么样的Javascript设计模式/方法
- 有什么方法可以放置一个触发器来在 CodeIgniter 的控制器函数中切换引导模式
- 揭示模块模式(JavaScript) - 这无法访问私有方法
- 为什么来自 Promise '.then' 方法的回调是反模式
- 有没有一种 AngularJS 方法来使用带有承诺返回的浏览器模式
- 当我在 Amazon S3 中将安静设置为 true 以在 deleteObjects() 方法中分析安静模式时会发生什
- 使用在另一个服务方法中声明为模块模式的服务方法之一
- 模块模式中的私有方法:类型错误:未定义不是一个函数
- 有没有更好的方法/模式来编写这个jQuery插件
- JavaScript 中构造函数的静态方法模式