daily pastebin goal
50%
SHARE
TWEET

Untitled

a guest Feb 21st, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. exports.createUser = functions.firestore
  2.   .document('users/{userId}').onCreate(() => {
  3.     const userDocRef = admin.firestore().collection('counters').doc('users');
  4.     return admin.firestore().runTransaction(function (transaction: any) {
  5.       return transaction.get(userDocRef).then(function (userDoc: any) {
  6.         if (!userDocRef.exists) {
  7.           userDocRef.set({ counter: 100 });
  8.         }
  9.         const newUserCount = userDoc.data().counter + 1;
  10.         transaction.update(userDocRef, { counter: newUserCount });
  11.       })
  12.     }).then(function () {
  13.       console.log('Transaction successfully committed!');
  14.     }).catch(function (error: any) {
  15.       console.log('Transaction failed: ', error);
  16.     });
  17.   });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top