The Code Crusader
The Code Crusader

Reputation: 33

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.

Upvotes: 3

Views: 728

Answers (2)

Gali Ainouz
Gali Ainouz

Reputation: 16

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

Upvotes: 0

DanteDX
DanteDX

Reputation: 1259

novu.trigger('event-name', {
   to: '1234',
   payload: {
     data: JSON.stringify([{ name: '1' }, { name: '2' }]) // this will convert your array into a string
   }
})

// when you fetch this payload, just do the following to parse the data

const fetchedData = JSON.parse(payload.data); // this is give you back your array

Upvotes: 0

Related Questions