CNode

mongoose如何事物回滚

Rrhino发布于13 年前最后回复13 年前2 回复7437 浏览0 收藏

comment.save(function(error, comment){ BlogModel.update({_id: blogId}, { $push: { comments: comment } }, function(error, number){ if(number == 0){ comment.remove(function(error){ if(!error){ options.error({msg: 'create comment fail: the speciall blog not exists'}) } }); }else{ DBUtil.handleQueryResult(error, comment, options); } }); 如果number等于0,此时comment实例新增成功,我还要手动将它delete,是否可以有一种方式让这个事物回滚

查看回复

回复 (2)

N
newghost#1·13 年前

为啥不反过来?

BlogModel.update({_id: blogId}, { $push: { comments: comment } }, function(error, number){
  if(number > 0){
      ....
      comment.save(function(error, comment){
R
rhino#2·13 年前

是,这样也可以,我后来用了一个Middleware: commentSchema.pre('save', function(next){ Blog.model.findById(this.blogId, function(error, blog){ if(error){ next(error); }else{ if(blog){ next(); }else{ var error = new Error(); error.msg = 'create comment fail: the speciall blog not exists'; next(error); } } }); });

参与回复
登录后即可参与回复。登录