@@ -4,8 +4,8 @@ function triggerSpecificDocument() {
4
4
// [START trigger_specific_document]
5
5
// Listen for any change on document `marie` in collection `users`
6
6
exports . myFunctionName = functions . firestore
7
- . document ( 'users/marie' ) . onWrite ( ( event ) => {
8
- // ... Your code here
7
+ . document ( 'users/marie' ) . onWrite ( ( change , context ) => {
8
+ // ... Your code here
9
9
} ) ;
10
10
// [END trigger_specific_document]
11
11
}
@@ -14,15 +14,15 @@ function triggerNewDocument() {
14
14
// [START trigger_new_document]
15
15
exports . createUser = functions . firestore
16
16
. document ( 'users/{userId}' )
17
- . onCreate ( event => {
18
- // Get an object representing the document
19
- // e.g. {'name': 'Marie', 'age': 66}
20
- const newValue = event . data . data ( ) ;
17
+ . onCreate ( ( snao , context ) => {
18
+ // Get an object representing the document
19
+ // e.g. {'name': 'Marie', 'age': 66}
20
+ const newValue = snap . data ( ) ;
21
21
22
- // access a particular field as you would any JS property
23
- const name = newValue . name ;
22
+ // access a particular field as you would any JS property
23
+ const name = newValue . name ;
24
24
25
- // perform desired operations ...
25
+ // perform desired operations ...
26
26
} ) ;
27
27
// [END trigger_new_document]
28
28
}
@@ -31,18 +31,18 @@ function triggerDocumentUpdated() {
31
31
// [START trigger_document_updated]
32
32
exports . updateUser = functions . firestore
33
33
. document ( 'users/{userId}' )
34
- . onUpdate ( event => {
35
- // Get an object representing the document
36
- // e.g. {'name': 'Marie', 'age': 66}
37
- const newValue = event . data . data ( ) ;
34
+ . onUpdate ( ( change , context ) => {
35
+ // Get an object representing the document
36
+ // e.g. {'name': 'Marie', 'age': 66}
37
+ const newValue = change . after . data ( ) ;
38
38
39
- // ...or the previous value before this update
40
- const previousValue = event . data . previous . data ( ) ;
39
+ // ...or the previous value before this update
40
+ const previousValue = change . before . data ( ) ;
41
41
42
- // access a particular field as you would any JS property
43
- const name = newValue . name ;
42
+ // access a particular field as you would any JS property
43
+ const name = newValue . name ;
44
44
45
- // perform desired operations ...
45
+ // perform desired operations ...
46
46
} ) ;
47
47
// [END trigger_document_updated]
48
48
}
@@ -51,12 +51,12 @@ function triggerDocumentDeleted() {
51
51
// [START trigger_document_deleted]
52
52
exports . deleteUser = functions . firestore
53
53
. document ( 'users/{userID}' )
54
- . onDelete ( event => {
55
- // Get an object representing the document prior to deletion
56
- // e.g. {'name': 'Marie', 'age': 66}
57
- const deletedValue = event . data . previous . data ( ) ;
54
+ . onDelete ( ( snap , context ) => {
55
+ // Get an object representing the document prior to deletion
56
+ // e.g. {'name': 'Marie', 'age': 66}
57
+ const deletedValue = snap . data ( ) ;
58
58
59
- // perform desired operations ...
59
+ // perform desired operations ...
60
60
} ) ;
61
61
// [END trigger_document_deleted]
62
62
}
@@ -65,13 +65,13 @@ function triggerDocumentAnyChange() {
65
65
// [START trigger_document_any_change]
66
66
exports . modifyUser = functions . firestore
67
67
. document ( 'users/{userID}' )
68
- . onWrite ( event => {
68
+ . onWrite ( ( change , context ) => {
69
69
// Get an object with the current document value.
70
70
// If the document does not exist, it has been deleted.
71
- const document = event . data . exists ? event . data . data ( ) : null ;
71
+ const document = change . after . exists ? change . after . data ( ) : null ;
72
72
73
73
// Get an object with the previous document value (for update or delete)
74
- const oldDocument = event . data . previous . data ( ) ;
74
+ const oldDocument = change . before . data ( ) ;
75
75
76
76
// perform desired operations ...
77
77
} ) ;
@@ -82,24 +82,24 @@ function readingData() {
82
82
// [START reading_data]
83
83
exports . updateUser = functions . firestore
84
84
. document ( 'users/{userId}' )
85
- . onUpdate ( event => {
86
- // Get an object representing the current document
87
- const newValue = event . data . data ( ) ;
85
+ . onUpdate ( ( change , context ) => {
86
+ // Get an object representing the current document
87
+ const newValue = change . after . data ( ) ;
88
88
89
- // ...or the previous value before this update
90
- const previousValue = event . data . previous . data ( ) ;
89
+ // ...or the previous value before this update
90
+ const previousValue = change . after . data ( ) ;
91
91
} ) ;
92
92
// [END reading_data]
93
93
}
94
94
95
- function readingDataWithGet ( event ) {
95
+ function readingDataWithGet ( snap ) {
96
96
// [START reading_data_with_get]
97
97
// Fetch data using standard accessors
98
- const age = event . data . data ( ) . age ;
99
- const name = event . data . data ( ) [ 'name' ] ;
98
+ const age = snap . data ( ) . age ;
99
+ const name = snap . data ( ) [ 'name' ] ;
100
100
101
101
// Fetch data using built in accessor
102
- const experience = event . data . get ( 'experience' ) ;
102
+ const experience = snap . get ( 'experience' ) ;
103
103
// [END reading_data_with_get]
104
104
}
105
105
@@ -108,25 +108,25 @@ function writingData() {
108
108
// Listen for updates to any `user` document.
109
109
exports . countNameChanges = functions . firestore
110
110
. document ( 'users/{userId}' )
111
- . onUpdate ( ( event ) => {
112
- // Retrieve the current and previous value
113
- const data = event . data . data ( ) ;
114
- const previousData = event . data . previous . data ( ) ;
115
-
116
- // We'll only update if the name has changed.
117
- // This is crucial to prevent infinite loops.
118
- if ( data . name == previousData . name ) return ;
119
-
120
- // Retrieve the current count of name changes
121
- let count = data . name_change_count ;
122
- if ( ! count ) {
123
- count = 0 ;
124
- }
125
-
126
- // Then return a promise of a set operation to update the count
127
- return event . data . ref . set ( {
128
- name_change_count : count + 1
129
- } , { merge : true } ) ;
111
+ . onUpdate ( ( change , context ) => {
112
+ // Retrieve the current and previous value
113
+ const data = change . after . data ( ) ;
114
+ const previousData = change . before . data ( ) ;
115
+
116
+ // We'll only update if the name has changed.
117
+ // This is crucial to prevent infinite loops.
118
+ if ( data . name == previousData . name ) return ;
119
+
120
+ // Retrieve the current count of name changes
121
+ let count = data . name_change_count ;
122
+ if ( ! count ) {
123
+ count = 0 ;
124
+ }
125
+
126
+ // Then return a promise of a set operation to update the count
127
+ return change . after . ref . set ( {
128
+ name_change_count : count + 1
129
+ } , { merge : true } ) ;
130
130
} ) ;
131
131
// [END writing_data]
132
132
}
@@ -136,11 +136,11 @@ function basicWildcard() {
136
136
// Listen for changes in all documents and all sub-collections
137
137
exports . useWildcard = functions . firestore
138
138
. document ( 'users/{userId}' )
139
- . onWrite ( ( event ) => {
140
- // If we set `/users/marie` to {name: "marie"} then
141
- // event .params.userId == "marie"
142
- // ... and ...
143
- // event.data .data() == {name: "Marie"}
139
+ . onWrite ( ( change , context ) => {
140
+ // If we set `/users/marie` to {name: "marie"} then
141
+ // context .params.userId == "marie"
142
+ // ... and ...
143
+ // change.after .data() == {name: "Marie"}
144
144
} ) ;
145
145
// [END basic_wildcard]
146
146
}
@@ -150,13 +150,13 @@ function multiWildcard() {
150
150
// Listen for changes in all documents and all subcollections
151
151
exports . useMultipleWildcards = functions . firestore
152
152
. document ( 'users/{userId}/{messageCollectionId}/{messageId}' )
153
- . onWrite ( ( event ) => {
154
- // If we set `/users/marie/incoming_messages/134` to {body: "Hello"} then
155
- // event .params.userId == "marie";
156
- // event .params.messageCollectionId == "incoming_messages";
157
- // event .params.messageId == "134";
158
- // ... and ...
159
- // event .data.data() == {body: "Hello"}
153
+ . onWrite ( ( change , context ) => {
154
+ // If we set `/users/marie/incoming_messages/134` to {body: "Hello"} then
155
+ // context .params.userId == "marie";
156
+ // context .params.messageCollectionId == "incoming_messages";
157
+ // context .params.messageId == "134";
158
+ // ... and ...
159
+ // context .data.data() == {body: "Hello"}
160
160
} ) ;
161
161
// [END multi_wildcard]
162
162
}
0 commit comments