Skip to content

Commit bd4e4be

Browse files
Update index.js
1 parent bfe484a commit bd4e4be

File tree

1 file changed

+66
-66
lines changed
  • firestore/extend-with-functions/functions

1 file changed

+66
-66
lines changed

firestore/extend-with-functions/functions/index.js

Lines changed: 66 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ function triggerSpecificDocument() {
44
// [START trigger_specific_document]
55
// Listen for any change on document `marie` in collection `users`
66
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
99
});
1010
// [END trigger_specific_document]
1111
}
@@ -14,15 +14,15 @@ function triggerNewDocument() {
1414
// [START trigger_new_document]
1515
exports.createUser = functions.firestore
1616
.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();
2121

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;
2424

25-
// perform desired operations ...
25+
// perform desired operations ...
2626
});
2727
// [END trigger_new_document]
2828
}
@@ -31,18 +31,18 @@ function triggerDocumentUpdated() {
3131
// [START trigger_document_updated]
3232
exports.updateUser = functions.firestore
3333
.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();
3838

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();
4141

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;
4444

45-
// perform desired operations ...
45+
// perform desired operations ...
4646
});
4747
// [END trigger_document_updated]
4848
}
@@ -51,12 +51,12 @@ function triggerDocumentDeleted() {
5151
// [START trigger_document_deleted]
5252
exports.deleteUser = functions.firestore
5353
.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();
5858

59-
// perform desired operations ...
59+
// perform desired operations ...
6060
});
6161
// [END trigger_document_deleted]
6262
}
@@ -65,13 +65,13 @@ function triggerDocumentAnyChange() {
6565
// [START trigger_document_any_change]
6666
exports.modifyUser = functions.firestore
6767
.document('users/{userID}')
68-
.onWrite(event => {
68+
.onWrite((change, context) => {
6969
// Get an object with the current document value.
7070
// 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;
7272

7373
// 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();
7575

7676
// perform desired operations ...
7777
});
@@ -82,24 +82,24 @@ function readingData() {
8282
// [START reading_data]
8383
exports.updateUser = functions.firestore
8484
.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();
8888

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();
9191
});
9292
// [END reading_data]
9393
}
9494

95-
function readingDataWithGet(event) {
95+
function readingDataWithGet(snap) {
9696
// [START reading_data_with_get]
9797
// 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'];
100100

101101
// Fetch data using built in accessor
102-
const experience = event.data.get('experience');
102+
const experience = snap.get('experience');
103103
// [END reading_data_with_get]
104104
}
105105

@@ -108,25 +108,25 @@ function writingData() {
108108
// Listen for updates to any `user` document.
109109
exports.countNameChanges = functions.firestore
110110
.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});
130130
});
131131
// [END writing_data]
132132
}
@@ -136,11 +136,11 @@ function basicWildcard() {
136136
// Listen for changes in all documents and all sub-collections
137137
exports.useWildcard = functions.firestore
138138
.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"}
144144
});
145145
// [END basic_wildcard]
146146
}
@@ -150,13 +150,13 @@ function multiWildcard() {
150150
// Listen for changes in all documents and all subcollections
151151
exports.useMultipleWildcards = functions.firestore
152152
.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"}
160160
});
161161
// [END multi_wildcard]
162162
}

0 commit comments

Comments
 (0)