[英]Inside Schema method scopes “this” is empty {} in Mongoose 4.4.12
在Schema方法中記錄到控制台時,對象“this”為“{}”。 這發生在一天前,我一直在閱讀教程和其他堆棧溢出問題,但沒有運氣,我找到了解決方案為什么。
這是我的模型:
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
關鍵字的語義,這可能會影響mongoose在內部處理回調的方式。
嘗試
UserSchema.pre("save", function(next) {
// ...
});
代替
UserSchema.pre("save", (next) => {
// ...
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.