Strongloop:在“保存之前"将操作钩子中的旧模型与新实例进行比较 [英] Strongloop : Compare old model with the new instance in operation hook 'before save'

查看:63
本文介绍了Strongloop:在“保存之前"将操作钩子中的旧模型与新实例进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在代码中实现了一个保存前"操作钩子,以将要保存的新实例与数据库中已有的旧实例进行比较. 为此,我将ctx.data中给出的值与数据库中查询给出的值进行了比较. 问题在于返回的值始终相似,就好像新实例已经保存在数据库中一样. 我是否完全错过了保存前"钩子的要点,或者有没有办法比较这两个值?

I implemented a "before save" operation hook in my code to compare the new instance about to be saved with the old one already in the database. For that, I compare the value given in the ctx.data with the one given by a query in the database. The problem is the returned values are always similar, as if the new instance has already been saved in the database. Have I totally missed the point of the "before save" hook, or is there a way to compare the two values ?

module.exports = function(app) {

var Like = app.models.Like;

Like.observe('before save', function(ctx, next) {

    var count = 0;
    if (ctx.instance) { // create operation
        console.log('create operation);
    }
    else { // update operation
        // Query for the existing model in db
        Like.findById(ctx.where.id,
            function(err, item) {
                if (err)
                    console.log(err);
                else {//compare query value and instance value
                    if (item.value != ctx.data.value) {
                        // Always false
                    }
                    else {
                        //Always true
                    }
                }
            }
        );
    }
    next();

我不明白为什么item.value总是与ctx.data.value相似,因为第一个应该是db中的实际值,第二个应该是要保存的值.

I can't understand why item.value always similar to ctx.data.value as the first one is supposed to be the actual value in the db and the second one the value about to be saved.

推荐答案

以这种方式在底部显示next()似乎不正确,并且可能给了足够的时间使保存真正发生在findById调用之前返回.呼叫next后,实际上可以进行保存,因此findById可以与您的保存竞争.

They way you have next() at the bottom doesn't seem right and might be giving enough time for the save to actually happen before the findById call returns. Once you call next the save can actually happen so findById can race with your save.

尝试这种方法,其中next()findById的回调中,这将阻止保存,直到完成比较为止.

Try it like this where your next() is within the callback from the findById which will block saving until you've done your comparison.

module.exports = function(app) {

var Like = app.models.Like;

Like.observe('before save', function(ctx, next) {

    var count = 0;
    if (ctx.instance) { // create operation
        console.log('create operation);
        next();
    }
    else { // update operation
        // Query for the existing model in db
        Like.findById(ctx.where.id,
            function(err, item) {
                if (err)
                    console.log(err);
                else {//compare query value and instance value
                    if (item.value != ctx.data.value) {
                        // Always false
                    }
                    else {
                        //Always true
                    }
                }
            next();
            }
        );
    }

这篇关于Strongloop:在“保存之前"将操作钩子中的旧模型与新实例进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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