hasOwnProperty 在 Mongoose 中的预验证中不起作用
hasOwnProperty not working in pre validate in Mongoose
我有一个架构 -
var PostSchema = new mongoose.Schema({
title: String,
link: String,
author: {type:String,required:true},
upvotes: {type: Number, default: 0},
comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }]
});
PostSchema.pre('validate',function(next){
console.log("Pre validate document printing - ");
console.log(this);
console.log("Pre validate called. Title : ",this._id);
console.log("Author is : ",this.author);
console.log("hasOwnProperty : ",this.hasOwnProperty("author"));
console.log("this.author==\'\' : ",this.author=='');
if(!(this.hasOwnProperty("author"))||this.author==''){
console.log("Here");
this.author="Hacked";
}
next();
});
在我的路线中 -
router.post('/posts', auth,function(req, res, next){
console.log(req.body);
console.log("Before - ");
var post = new Post(req.body);
post.upvotes="2";
console.log(post);
post.author="Meseeks";
console.log("After - ");
console.log(post);
console.log("Creating author");
//post.author = req.payload.username;
post.save(function(err, post){
if(err){return next(err);}
res.json(post);
});
});
Nodejs 日志 - 打印此 -
Before -
{ title: 'im mr meeseeks',
link: null,
_id: 589f0f3ddf781803b459dc00,
comments: [],
upvotes: 2 }
After -
{ author: 'Meseeks',
title: 'im mr meeseeks',
link: null,
_id: 589f0f3ddf781803b459dc00,
comments: [],
upvotes: 2 }
Creating author
Pre validate document printing -
{ author: 'Meseeks',
title: 'im mr meeseeks',
link: null,
_id: 589f0f3ddf781803b459dc00,
comments: [],
upvotes: 2 }
Pre validate called. Title : 589f0f3ddf781803b459dc00
Author is : Meseeks
hasOwnProperty : false
this.author=='' : false
Here
Post validate called. Title : 589f0f3ddf781803b459dc00
Pre save called. Title : 589f0f3ddf781803b459dc00
Post save called. Title : 589f0f3ddf781803b459dc00
我正在我的路由器中手动设置作者。但是,预初始化函数无法验证对象中是否存在 属性 "author"。这怎么可能?我做错了什么吗??
如何检查 属性 是否存在于 pre init 中?
此时代码中 "this" 不是对象,只是原型。提供了更多解释和解决方法 。
我有一个架构 -
var PostSchema = new mongoose.Schema({
title: String,
link: String,
author: {type:String,required:true},
upvotes: {type: Number, default: 0},
comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }]
});
PostSchema.pre('validate',function(next){
console.log("Pre validate document printing - ");
console.log(this);
console.log("Pre validate called. Title : ",this._id);
console.log("Author is : ",this.author);
console.log("hasOwnProperty : ",this.hasOwnProperty("author"));
console.log("this.author==\'\' : ",this.author=='');
if(!(this.hasOwnProperty("author"))||this.author==''){
console.log("Here");
this.author="Hacked";
}
next();
});
在我的路线中 -
router.post('/posts', auth,function(req, res, next){
console.log(req.body);
console.log("Before - ");
var post = new Post(req.body);
post.upvotes="2";
console.log(post);
post.author="Meseeks";
console.log("After - ");
console.log(post);
console.log("Creating author");
//post.author = req.payload.username;
post.save(function(err, post){
if(err){return next(err);}
res.json(post);
});
});
Nodejs 日志 - 打印此 -
Before -
{ title: 'im mr meeseeks',
link: null,
_id: 589f0f3ddf781803b459dc00,
comments: [],
upvotes: 2 }
After -
{ author: 'Meseeks',
title: 'im mr meeseeks',
link: null,
_id: 589f0f3ddf781803b459dc00,
comments: [],
upvotes: 2 }
Creating author
Pre validate document printing -
{ author: 'Meseeks',
title: 'im mr meeseeks',
link: null,
_id: 589f0f3ddf781803b459dc00,
comments: [],
upvotes: 2 }
Pre validate called. Title : 589f0f3ddf781803b459dc00
Author is : Meseeks
hasOwnProperty : false
this.author=='' : false
Here
Post validate called. Title : 589f0f3ddf781803b459dc00
Pre save called. Title : 589f0f3ddf781803b459dc00
Post save called. Title : 589f0f3ddf781803b459dc00
我正在我的路由器中手动设置作者。但是,预初始化函数无法验证对象中是否存在 属性 "author"。这怎么可能?我做错了什么吗??
如何检查 属性 是否存在于 pre init 中?
此时代码中 "this" 不是对象,只是原型。提供了更多解释和解决方法