1

I have a loopback 2.x app, in which I have a model Conversation and a model Message, with a relationship "Conversation has many messages". I want to customize the response for POST conversations/:id/messages with a json response different than the default, say {status: 'success'}. I tried to use remote hook for the method __create__messages, but it did not work:

Conversation.afterRemote('__create__messages', function(ctx, next) {
  ctx.result.data = {
    success: 'yes'
  };
  next();
});

This still returns the default response. How can I return a custom json for a remote method? I have seen examples only for all models, or for all methods: multiple models, multiple methods

Community
  • 1
  • 1
Hristo Vrigazov
  • 1,357
  • 2
  • 12
  • 20

1 Answers1

0

Maybe you can try a version of following code below. Also, I think you are meaning to to manipulate data before the method finishes, not after. If you wait, the response will already be created, preventing your intended goal. Let me know if this works (replace with methods that will work for your use case).

   Conversation.observe('before save', function(context, next) {
        var instance = context.instance || context.data;
        if (!instance) return next();
        // Your code here
        next();
      });
sirrele
  • 171
  • 1
  • 6