13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
- ' use strict' ;
16
+ " use strict" ;
17
17
18
18
// [START all]
19
19
// [START import]
20
- // The Cloud Functions for Firebase SDK to create Cloud Functions and set up triggers.
21
- const { logger} = require ( ' firebase-functions' ) ;
22
- const { onRequest} = require ( ' firebase-functions/v2/https' ) ;
23
- const { onDocumentCreated} = require ( ' firebase-functions/v2/firestore' ) ;
20
+ // The Cloud Functions for Firebase SDK to create Cloud Functions and triggers.
21
+ const { logger} = require ( " firebase-functions" ) ;
22
+ const { onRequest} = require ( " firebase-functions/v2/https" ) ;
23
+ const { onDocumentCreated} = require ( " firebase-functions/v2/firestore" ) ;
24
24
25
25
// The Firebase Admin SDK to access Firestore.
26
26
const { initializeApp} = require ( "firebase-admin/app" ) ;
@@ -30,42 +30,46 @@ initializeApp();
30
30
// [END import]
31
31
32
32
// [START addmessage]
33
- // Take the text parameter passed to this HTTP endpoint and insert it into
33
+ // Take the text parameter passed to this HTTP endpoint and insert it into
34
34
// Firestore under the path /messages/:documentId/original
35
35
// [START addmessageTrigger]
36
36
exports . addmessage = onRequest ( async ( req , res ) => {
37
- // [END addmessageTrigger]
37
+ // [END addmessageTrigger]
38
38
// Grab the text parameter.
39
39
const original = req . query . text ;
40
40
// [START adminSdkAdd]
41
41
// Push the new message into Firestore using the Firebase Admin SDK.
42
- const writeResult = await getFirestore ( ) . collection ( 'messages' ) . add ( { original : original } ) ;
42
+ const writeResult = await getFirestore ( )
43
+ . collection ( "messages" )
44
+ . add ( { original : original } ) ;
43
45
// Send back a message that we've successfully written the message
44
46
res . json ( { result : `Message with ID: ${ writeResult . id } added.` } ) ;
45
47
// [END adminSdkAdd]
46
48
} ) ;
47
49
// [END addmessage]
48
50
49
51
// [START makeuppercase]
50
- // Listens for new messages added to /messages/:documentId/original and creates an
51
- // uppercase version of the message to /messages/:documentId/uppercase
52
+ // Listens for new messages added to /messages/:documentId/original
53
+ // and saves an uppercased version of the message
54
+ // to /messages/:documentId/uppercase
52
55
// [START makeuppercaseTrigger]
53
- exports . makeuppercase = onDocumentCreated ( ' /messages/{documentId}' , ( event ) => {
54
- // [END makeuppercaseTrigger]
55
- // [START makeUppercaseBody]
56
- // Grab the current value of what was written to Firestore.
57
- const original = event . data . data ( ) . original ;
56
+ exports . makeuppercase = onDocumentCreated ( " /messages/{documentId}" , ( event ) => {
57
+ // [END makeuppercaseTrigger]
58
+ // [START makeUppercaseBody]
59
+ // Grab the current value of what was written to Firestore.
60
+ const original = event . data . data ( ) . original ;
58
61
59
- // Access the parameter `{documentId}` with `context.params`
60
- logger . log ( 'Uppercasing' , context . params . documentId , original ) ;
61
-
62
- const uppercase = original . toUpperCase ( ) ;
63
-
64
- // You must return a Promise when performing asynchronous tasks inside a Functions such as
65
- // writing to Firestore.
66
- // Setting an 'uppercase' field in Firestore document returns a Promise.
67
- return event . data . ref . set ( { uppercase} , { merge : true } ) ;
68
- // [END makeUppercaseBody]
69
- } ) ;
62
+ // Access the parameter `{documentId}` with `event.params`
63
+ logger . log ( "Uppercasing" , event . params . documentId , original ) ;
64
+
65
+ const uppercase = original . toUpperCase ( ) ;
66
+
67
+ // You must return a Promise when performing
68
+ // asynchronous tasks inside a function
69
+ // such as writing to Firestore.
70
+ // Setting an 'uppercase' field in Firestore document returns a Promise.
71
+ return event . data . ref . set ( { uppercase} , { merge : true } ) ;
72
+ // [END makeUppercaseBody]
73
+ } ) ;
70
74
// [END makeuppercase]
71
75
// [END all]
0 commit comments