I would like to increment a counter in my pre save callback. I found this stackoverflow really useful to do so: Does Mongoose support the Mongodb `findAndModify` method?
What I would like to do is use the findAndModify method. But when I implement the statics my squence of callbacks is not as expected. I do always and pre save and then execute findAndModify but I would like do execute findAndModify in between start and end of the pre save hook. If I define a general method with a callback it works as expected.
worked also with the done parameter of the pre-save hook without any different result
What do I miss here
My code looks like this:
var mongoose = require('mongoose');
var should = require('should');
mongoose.connect("localhost","test_db");
var CommentSchema = new mongoose.Schema({
content: {type:String},
created_at: {type:Date, default:Date.now},
_post:{type:mongoose.Schema.ObjectId,ref:'Post'}
});
var PostSchema = new mongoose.Schema({
title: {type:String},
content: {type:String},
comments: [{type:mongoose.Schema.ObjectId, ref:'Comment'}],
counter: {type:Number}
});
PostSchema.statics.findAndModify= function(query,sort,doc,options,callback){
return this.collection.findAndModify(query,sort,doc,options,callback);
}
PostSchema.statics.test_me = function(clb){
console.log("test_me");
clb();
}
CommentSchema.pre('save',function(next,done){
console.log("enter pre save comment");
if(this.isNew){
Post.findAndModify({_id:this._post},[],{$inc:{count:1}},{new:true},function(err,post){
console.log("enter find-and-modify!");
console.log(post);
});
Post.test_me(function(){
console.log("callback of test_me");
});
console.log("exit pre save comment");
next();
}
});
var Post = mongoose.model('Post',PostSchema);
var Comment = mongoose.model('Comment',CommentSchema);
var post = new Post({title:"hello world"});
var comment = new Comment({content:"1st comment",_post:post});
post.comments.push(comment);
var id = post.id;
console.log(id);
post.save(function(err){
comment.save(function(err){
Post.find({_id:id })
.populate('comments')
.exec(function(err,result){
console.log("--------------- result -----------------");
console.log(result);
});
});
});
This is the result from my command-line:
5049f0d2e21547430a000001
enter pre save comment
test_me
callback of test_me
exit pre save comment
enter find-and-modify!
{ __v: 0,
_id: 5049f0d2e21547430a000001,
comments: [ 5049f0d2e21547430a000002 ],
count: 1,
title: 'hello world' }
--------------- result -----------------
[ { __v: 0,
_id: 5049f0d2e21547430a000001,
count: 1,
title: 'hello world',
comments:
[ { content: '1st comment',
_post: 5049f0d2e21547430a000001,
_id: 5049f0d2e21547430a000002,
__v: 0,
created_at: Fri Sep 07 2012 15:04:18 GMT+0200 (CEST) } ] } ]
EDIT: I do not want to know how to execute findAndModify with test_me in a sequence. I want to know why findAndMody enters after pre-saved is finished. Even it is embedded and should work as demonstrated with the test_me method. So the test_me method should illustrate that a async method should work nested... but findAndModify does not... like my command-line output shows...it always enters findAndModify after pre-save exits even when I use the done() callback...