Search code examples
typescriptnotificationsopen-sourcenovu

Sending and Displaying an Array of Objects as Payload in Novu Platform


Is it possible to send an array of objects as the payload when triggering an event through code in the novu platform? If so, how can we properly format and display the elements of this array in a template?

novu.trigger('event-name', {
   to: '1234',
   payload: {
     data: [{ name: '1' }, { name: '2' }]
   }
})

I'm unsure how to get to it, but my preferred outcome is sending the array as a payload while triggering through code.


Solution

  • Your payload is in the right format for novu.

    For your question about how to show it in the template - it's basically how you would do it with handlebars. So your example will correspond in the template editor to something like this:

    {{#each data}} Hello {{name}}! {{/each}}

    You could also take a look at this section in novu's documentation.

    Another small example:

        {{#if isWinner}}  {{winner.name}} , claim your prize {{url}} {{/if}} 
        <ul>{{#each players}} <li>{{name}} : {{score}}  points</li> {{/each}}</ul>
    

    novu.trigger('event-name', {
      to: '1234',
      payload: {
        isWinner: true,
        players: [{
            name: 'Richard',
            score: 6,
          },
          {
            name: 'John',
            score: 8,
          },
        ],
        winner: {
          name: 'Elizabeth',
        },
        url: 'here',
      }
    })

    And this will how it would look