在更新后中间件中发现不正确的文档修订号 [英] Incorrect document revision number found in post update middleware

查看:113
本文介绍了在更新后中间件中发现不正确的文档修订号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Mongoose插件,可用于增加文档修订号(__v)以及创建修订本身.该插件涵盖了文档Doc.save()中间件功能,以及查询updatefindOneAndUpdate中间件功能.

I have a Mongoose plugin which I use to increment the documents revision number (__v), as well as create revision itself. The plugin covers the Documents Doc.save() middleware function, as well as the Query update and findOneAndUpdate middleware functions.

module.exports = ( schema, options ) => {
    _.forEach( [ 'save','update', 'findOneAndUpdate' ], query => {
        // Since the Doc.save() middleware is the only document update middleware function, just isolate that one
        if( query === 'save' )
            schema.pre( query, function( next ) {
                this.increment()
                next()
            } )

        // The rest are query updates
        else
            schema.pre( query, function() {
                this.update( {}, { $inc: { __v: 1 } } )
            })


        // Create revisions for each document update
        schema.post( query, docData => {
            Mongoose.models.Revision.createRevision( {
                docsId: this._id,
                revision: docData.__v, // <-- This is the wrong number. It's one less than it should be
                document: { /* Stuff.. */ }
                // More stuff
            }, ( err, revision ) => {
                // CB Stuff
            })
        })
    })
}

因此,大多数情况下都可以按预期进行.对于文档和查询交互,文档的__v值都会增加,同时也会创建修订文档.我所坚持的部分与查询中间件功能updatefindOneAndUpdate有关.即使__v是通过 pre 事件在文档中更新的,但 post 事件中的this.__v值似乎也看不到更新后的值.因此,这意味着将创建修订并引用了错误的文档修订号.

So this mostly works as expected. The __v value of the document gets incremented for both document and query interactions, and the revision documents get created as well. The part that i'm stuck on, is related to the Query middleware functions, update and findOneAndUpdate. Even though the __v gets updated in the document via the pre event, the this.__v value in the post event doesn't seem to see the updated value. So that means the revision gets created and references the incorrect revision number of the document.

这太奇怪了,因为实际上当我在数据库中查看文档__v 时会对其进行更新,但是当我 console.log 时, 帖子 update中的this.__v.它会在更新之前看到修订号.

This is just super weird, because the documents __v does in fact get updated when I look at it in the database, but when I console.log the this.__vin the post update.. it sees the revision number prior to it being updated..

对于临时修复,我只是手动对其进行了递增,如果它具有查询MW函数的话:

For a temporary fix, I just increment it manually, if its a query MW function:

schema.post( query, docData => {
    Mongoose.models.Revision.createRevision( {
        docsId: this._id,
        revision: ( query === 'save' // Temporary fix..
            ? docData.__v
            : docData.__v+1 ) // Add +1 if its a query function 
        document: { /* Stuff.. */ }
        // More stuff
    }, ( err, revision ) => {
        // CB Stuff
    })
})

但是很明显,这只是一个创可贴,所以如果有一个真正的解决办法,那就太好了

But obviously, this is just a bandaid, so if theres a real fix for this, that would be great

有什么主意吗?

推荐答案

即使__v是通过 pre 事件在文档中更新的,但post事件中的this.__v值似乎也看不到更新的值.

Even though the __v gets updated in the document via the pre event, the this.__v value in the post event doesn't seem to see the updated value.

最可能的原因是,用于updatefindOneAndUpdate的中间件没有使用异步回调来等待操作完成,然后再继续执行(您为 did 的某些操作) >中间件).

That's most likely because your middleware for update and findOneAndUpdate isn't using an async callback to wait for the action to finish before continuing( something that you did implement for the save middleware).

因此请使其使用回调:

schema.pre( query, function(next) {
  this.update( {}, { $inc: { __v : 1 } }, next);
})

这篇关于在更新后中间件中发现不正确的文档修订号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆