简体   繁体   中英

How to avoid service multiple time run in hook using feathers.js?

I'm facing problem in feathers service in feathers hook. Exactly the problem is i'm using feather service in feather hook and when i call service in hook it run multiple times so that memory issue is to be happen. my question is how to avoid service multiple run in hook.

function orders(hook){
  return new Promise((resolve,reject) =>{
      hook.app.service('orders')
        .find(hook.app.query)
        .then(result => {
          resolve(result.data)
        }).catch(e =>{
        reject(e)
      })
  })
}

my expected solution is the service should be run in single time at hook.

A service method ideally shouldn't call itself in a hook but if you do, you will need a breaking condition so that it doesn't keep calling itself in an infinite loop. This can be done by eg passing a parameter that will skip the self-referential call if it is not set:

app.service('myservice').hooks({
  before: {
    find(hook) {
      if(!hook.params.fromOtherHook) {

        const newParams = Object.assign({
          fromOtherHook: true
        }, hook.params);

        return hook.service.find(newParams);
      }
    }
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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