Search code examples
loopbackjsloopback

Pass context in 'persist' to 'after save'


I need to pass context from a Operation Hook (persist) to another (after save), I know the existence of the ctx.hookState but its not working.

ZZ.observe('persist', (ctx, next) => {
      ctx.hookState = "pass this";
      next();
    }).catch(err => next(err));
  });

ZZ.observe('after save', (ctx, next) => {
  console.log(ctx.hookState);
  next()
});

I don't get anything in console.log(ctx.hookState). What I'm doing wrong?

Thanks.


Solution

  • You shouldn't overwrite hookState

    You can do like this :

    ZZ.observe('persist', (ctx, next) => {
          ctx.hookState.foo = "pass this";
          next();    
      });
    
    ZZ.observe('after save', (ctx, next) => {
      console.log(ctx.hookState.foo);
      next()
    });