|
17 | 17 |
|
18 | 18 | const functions = require('firebase-functions'); |
19 | 19 | const admin = require('firebase-admin'); |
20 | | -admin.initializeApp(functions.config().firebase); |
| 20 | +admin.initializeApp(); |
21 | 21 |
|
22 | 22 | // Keeps track of the length of the 'likes' child list in a separate property. |
23 | | -exports.countlikechange = functions.database.ref('/posts/{postid}/likes/{likeid}').onWrite((event) => { |
24 | | - const collectionRef = event.data.ref.parent; |
25 | | - const countRef = collectionRef.parent.child('likes_count'); |
| 23 | +exports.countlikechange = functions.database.ref('/posts/{postid}/likes/{likeid}').onWrite( |
| 24 | + (change) => { |
| 25 | + const collectionRef = change.after.ref.parent; |
| 26 | + const countRef = collectionRef.parent.child('likes_count'); |
26 | 27 |
|
27 | | - let increment; |
28 | | - if (event.data.exists() && !event.data.previous.exists()) { |
29 | | - increment = 1; |
30 | | - } else if (!event.data.exists() && event.data.previous.exists()) { |
31 | | - increment = -1; |
32 | | - } else { |
33 | | - return null; |
34 | | - } |
| 28 | + let increment; |
| 29 | + if (change.after.exists() && !change.after.previous.exists()) { |
| 30 | + increment = 1; |
| 31 | + } else if (!change.after.exists() && change.after.previous.exists()) { |
| 32 | + increment = -1; |
| 33 | + } else { |
| 34 | + return null; |
| 35 | + } |
35 | 36 |
|
36 | | - // Return the promise from countRef.transaction() so our function |
37 | | - // waits for this async event to complete before it exits. |
38 | | - return countRef.transaction((current) => { |
39 | | - return (current || 0) + increment; |
40 | | - }).then(() => { |
41 | | - return console.log('Counter updated.'); |
42 | | - }); |
43 | | -}); |
| 37 | + // Return the promise from countRef.transaction() so our function |
| 38 | + // waits for this async event to complete before it exits. |
| 39 | + return countRef.transaction((current) => { |
| 40 | + return (current || 0) + increment; |
| 41 | + }).then(() => { |
| 42 | + return console.log('Counter updated.'); |
| 43 | + }); |
| 44 | + }); |
44 | 45 |
|
45 | 46 | // If the number of likes gets deleted, recount the number of likes |
46 | | -exports.recountlikes = functions.database.ref('/posts/{postid}/likes_count').onWrite((event) => { |
47 | | - if (!event.data.exists()) { |
48 | | - const counterRef = event.data.ref; |
49 | | - const collectionRef = counterRef.parent.child('likes'); |
| 47 | +exports.recountlikes = functions.database.ref('/posts/{postid}/likes_count').onDelete((snap) => { |
| 48 | + const counterRef = snap.ref; |
| 49 | + const collectionRef = counterRef.parent.child('likes'); |
50 | 50 |
|
51 | | - // Return the promise from counterRef.set() so our function |
52 | | - // waits for this async event to complete before it exits. |
53 | | - return collectionRef.once('value') |
| 51 | + // Return the promise from counterRef.set() so our function |
| 52 | + // waits for this async event to complete before it exits. |
| 53 | + return collectionRef.once('value') |
54 | 54 | .then((messagesData) => counterRef.set(messagesData.numChildren())); |
55 | | - } |
56 | | - return null; |
57 | 55 | }); |
0 commit comments