0

保存前のコールバックでカウンターをインクリメントしたいと思います。これを行うには、このスタックオーバーフローが非常に便利であることがわかりまし た。Mongoose は Mongodb の `findAndModify` メソッドをサポートしていますか?

私がやりたいことは、findAndModify メソッドを使用することです。しかし、静的を実装すると、コールバックのシーケンスが期待どおりになりません。私は常にプリセーブしてからfindAndModifyを実行しますが、プリセーブフックの開始と終了の間にfindAndModifyを実行したいと思います。コールバックを使用して一般的なメソッドを定義すると、期待どおりに機能します。

異なる結果なしで、事前保存フックの完了パラメーターでも機能しました

ここで何が恋しいですか

私のコードは次のようになります。

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);
          });
    });
  });

これは私のコマンドラインからの結果です:

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) } ] } ]

編集: シーケンスで test_me を使用して findAndModify を実行する方法を知りたくありません。pre-saved が終了した後に findAndMody が入る理由を知りたいです。それも埋め込まれており、test_me メソッドで示されているように動作するはずです。したがって、test_meメソッドは、非同期メソッドがネストされて機能することを示す必要があります...しかし、findAndModifyはそうではありません...私のコマンドライン出力が示すように...done()を使用しても、事前保存終了後に常にfindAndModifyに入ります折り返し電話...

4

1 に答える 1

0

非同期を扱っているので、実行されます

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();
}

次の処理に移る前に、実行または応答を待たずに次々と実行されます。Post.findAndModify() の実行には時間がかかるため、コールバック関数内のすべてのものは、コールバック関数の外にあるすべてのものの後に実行されます。

もっと順番に実行したい場合

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();
    });
  });     
}
于 2012-09-07T19:41:20.900 に答える