65649

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

Question:

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.

Answer1:

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 e.g. 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); } } } });

Recommend

  • Selecting and check if user exist in table without foreach in laravel blade
  • Property 'catch' does not exist on type 'PromiseLike
  • Implement a writable serializer for multilevel nested relationships in django rest framework
  • SUM and GROUP BY in xquery with 1 xml file
  • I have a modal that opens a second modal and cannot close the first modal if open then close the sec
  • Why is my req.body always empty on POST?
  • Why is it still possible to insert a foreign key that doesn't exist?
  • Cannot access Public method in Web Control's Page_Load event
  • How to use jQuery's $.post() method with async/await and typescript
  • Overlapping controls in Windows XP
  • how to avoid repetitive constructor in children
  • How to get Eclipse Oxygen to run on Java 9
  • Can you perform a UNION without a subquery in SQLAlchemy?
  • Setting up SourceTree to merge unity3d scenes with UnityYAMLMerge
  • Unity3D & Android: Difference between “UnityMain” and “main” threads?
  • PostgreSQL Query without WHERE only ORDER BY and LIMIT doesn't use index
  • Ajax Loaded meta Tags
  • Control modification in presentation layer
  • Xamarin Forms - UWP Fonts
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • Display Images one by one with next and previous functionality
  • php design question - will a Helper help here?
  • Arrays break string types in Julia
  • SQL merge duplicate rows and join values that are different
  • AngularJs get employee from factory
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • How can I remove ASP.NET Designer.cs files?
  • python draw pie shapes with colour filled
  • Is there any way to bind data to data.frame by some index?
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Reading document lines to the user (python)
  • Binding checkboxes to object values in AngularJs
  • How can i traverse a binary tree from right to left in java?
  • How can I use `wmic` in a Windows PE script?
  • Net Present Value in Excel for Grouped Recurring CF
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?