简体   繁体   English

由于缺少Access / Loaded Hook的上下文对象(LoopBack3)中的模型实例,无法更改所需的属性值

[英]Unable to alter desired attribute value due to missing model instance in Access/Loaded Hook's context objects(LoopBack3)

I want to alter an attribute in someModel, whenever a find is called over this model. 每当在此模型上调用查找时,我都想更改someModel中的属性。 As I can't use remote Hooks as find is not a remote method, rather built in, and in operational hooks find/findOne only trigger access and loaded hooks, and as my research, they do not return the model instance in their ctx (or if they do, I would like to know where), I want to do something like: 由于我不能使用远程Hook,因为find不是远程方法,而是内置的,并且在操作Hook中find / findOne仅触发访问和已加载的Hook,并且根据我的研究,它们不会在ctx中返回模型实例(或者如果他们愿意,我想知道在哪里),我想做些类似的事情:

modelName.observe('loaded', function (ctx, next) {
      ctx.someModel_instance.updateAttribute(someCount, value
            ,function(err, instance){
             if (err) next(err)
                else{
                      console.log("done")
                 }

      });


} 

Work Around: As loaded does not return model instance but it does return ctx.data , in which it returns a copy of the data in your model, If you happen to have a unique ID in your model so you can fetch model instance by findById and can persistently access/alter the attribute of the said model. 解决方法: loaded不会返回模型实例,但会返回ctx.data ,其中它返回模型中数据的副本。如果您在模型中碰巧有一个唯一ID ,则可以通过findById来获取模型实例并且可以持久访问/更改所述模型的属性。 eg: 例如:

modelName.observe('loaded', function (ctx, next) {
        modelName.findOne({
          where: {id : ctx.data.id},
          },function(err, someModel_instance){
                    if (err) next(err)
                    else{   
                        someModel_instance.updateAttribute(someCount,value
                            , function(err, instance){
                                console.log(done)
                        });     
                        }
                });
                next();
} );

This will do the trick, but the problem will be non-stop recursion that it causes. 这将达到目的,但是问题将是它引起的不间断的递归。 As findOne and updateAttribute will trigger the loaded hook again and so on. 因为findOneupdateAttribute将再次触发loaded hook ,依此类推。 This can be resolved by using ctx.options field which acts like an empty container and can be used to store flags. 这可以通过使用ctx.options字段来解决, ctx.options字段的作用类似于一个空容器,可以用来存储标志。 eg: 例如:

modelName.observe('loaded', function (ctx, next) {
    if(ctx.options && !ctx.options.alreadyFound){

        modelName.findOne({
          where: {id : ctx.data.id},
          },{alreadyFound = true}, function(err, someModel_instance){
                    if (err) next(err)
                    else{   
                        someModel_instance.updateAttribute(someCount,value
                            ,{alreadyFound = true}, function(err, instance){
                                console.log(done)
                        });     
                        }
                });

    }
    next();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM