|
1
|
import { TypeList } from "docs-ui" |
|
2
|
|
|
3
|
export const metadata = { |
|
4
|
title: `${pageNumber} Event Data Payload`, |
|
5
|
} |
|
6
|
|
|
7
|
# {metadata.title} |
|
8
|
|
|
9
|
In this chapter, you'll learn how subscribers receive an event's data payload. |
|
10
|
|
|
11
|
## Access Event's Data Payload |
|
12
|
|
|
13
|
When events are emitted, they’re emitted with a data payload. |
|
14
|
|
|
15
|
The object that the subscriber function receives as a parameter has an `event` property, which is an object holding the event payload in a `data` property with additional context. |
|
16
|
|
|
17
|
For example: |
|
18
|
|
|
19
|
export const highlights = [ |
|
20
|
["7", "event", "The event details."], |
|
21
|
["8", "{ id: string }", "The type of expected data payloads."], |
|
22
|
] |
|
23
|
|
|
24
|
```ts title="src/subscribers/product-created.ts" highlights={highlights} collapsibleLines="1-5" expandButtonLabel="Show Imports" |
|
25
|
import type { |
|
26
|
SubscriberArgs, |
|
27
|
SubscriberConfig, |
|
28
|
} from "@medusajs/framework" |
|
29
|
|
|
30
|
export default async function productCreateHandler({ |
|
31
|
event, |
|
32
|
}: SubscriberArgs<{ id: string }>) { |
|
33
|
const productId = event.data.id |
|
34
|
console.log(`The product ${productId} was created`) |
|
35
|
} |
|
36
|
|
|
37
|
export const config: SubscriberConfig = { |
|
38
|
event: "product.created", |
|
39
|
} |
|
40
|
``` |
|
41
|
|
|
42
|
The `event` object has the following properties: |
|
43
|
|
|
44
|
<TypeList types={[ |
|
45
|
{ |
|
46
|
name: "data", |
|
47
|
type: "`object`", |
|
48
|
description: "The data payload of the event. Its properties are different for each event." |
|
49
|
}, |
|
50
|
{ |
|
51
|
name: "name", |
|
52
|
type: "string", |
|
53
|
description: "The name of the triggered event." |
|
54
|
}, |
|
55
|
{ |
|
56
|
name: "metadata", |
|
57
|
type: "`object`", |
|
58
|
description: "Additional data and context of the emitted event.", |
|
59
|
optional: true |
|
60
|
}, |
|
61
|
]} sectionTitle="Access Event's Data Payload" /> |
|
62
|
|
|
63
|
This logs the product ID received in the `product.created` event’s data payload to the console. |
|
64
|
|
|
65
|
{/* --- |
|
66
|
|
|
67
|
## List of Events with Data Payload |
|
68
|
|
|
69
|
Refer to [this reference](!resources!/references/events) for a full list of events emitted by Medusa and their data payloads. */} |