documentation icon indicating copy to clipboard operation
documentation copied to clipboard

[Request]: lifecycle documentation improvement

Open dieguezz opened this issue 3 years ago • 1 comments

Summary

when updating an item through a lifecycle hook, for some reason you cannot do it from a third function.

ie: this will not update the data

  function foo(event) {
    event.params.data.foo = 'foo'
  }
  beforeUpdate(event) {
    foo(event)
  }

Instead you need to do something like:

  function foo(event) { 
    return event.params.data.whatever ? 'foo' : 'bar'
  }
  beforeUpdate(event) {
    event.params.data.foo = foo(event)
  }

And js works passing a reference, as you can check here So this is definately weird and should be documented if that is the expected behavior

Why is it needed?

Its needed because if for example you want to run the same logic under two hooks, its normal that you want to abstract this logic like shown in first example instead of:

module.exports = {
  beforeUpdate(event) {
    event.params.data.foo = await beforeUpdateOrCreate(event);
  },
  beforeCreate(event) {
    event.params.data.foo = await beforeUpdateOrCreate(event);
  },
};

Suggested solution(s)

Just fix the behavior or document it

Related issue(s)/PR(s)

No response

dieguezz avatar Apr 10 '22 23:04 dieguezz

Thank you for opening this issue, and apologies for the late reply. Would you like to update yourself? Community contributions to the Strapi documentation are always welcome.

Feel free to use the various contribution resources below:

Thank you!

pwizla avatar Jun 02 '22 14:06 pwizla