I am new to Cloud Firestore , I want to create a function when a new user has been been added to Firestore, it generates a unique random number and save it in a document. How I can achieve it using Google Cloud Console?

- 79,263
- 10
- 95
- 121

- 5,249
- 6
- 50
- 88
-
Are you talking about [Firebase Console](https://console.firebase.google.com/)? – Alex Mamo May 29 '18 at 17:31
-
Yes @AlexMamo .. – DareDevil May 29 '18 at 17:43
-
I wanted to know the opposite, deleting data when a user is deleted. My own answer to my question might give you some pointers. https://stackoverflow.com/questions/49367260/firestore-delete-all-user-data-on-account-delete-using-a-firebase-function – CodeChimp May 29 '18 at 17:51
1 Answers
You can achieve this, by simply pressing the ADD COLLECTION
button which looks like this:
A new pop-up will come up, which looks like this:
Name your collection in every way you want. I have named it Products
. Press NEXT
and a new pop-up will come up again:
Leave the Document id
field blank. Choose a filed (I named id productName) and a value of type String named Milk
.
This is the result:
You can use Cloud Functions for Firebase, so every time a new entry (document) is added on a specific location in your Cloud Firebase database, you'll be able to trigger another frunction. For that, I'll give you an example. Let's assume we want to send a notification to a user when something happens.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.yourFunction = functions.firestore.document("notifications/{uid}/userNotifications/{notificationId}").onWrite(event => {
const uid = event.params.uid;
const notificationId = event.params.notificationId;
return admin.firestore().collection("notifications").doc(uid).collection("userNotifications").doc(notificationId).get().then(queryResult => {
const uid = queryResult.data().senderUid;
const notificationMessage = queryResult.data().notificationMessage;
const fromUser = admin.firestore().collection("users").doc(senderUid).get();
const toUser = admin.firestore().collection("users").doc(uid).get();
return Promise.all([fromUser, toUser]).then(result => {
const fromUserName = result[0].data().userName;
const toUserName = result[1].data().userName;
const tokenId = result[1].data().tokenId;
const notificationContent = {
notification: {
title: "Your Title",
body: notificationMessage,
icon: "default",
sound : "default"
}
};
return admin.messaging().sendToDevice(tokenId, notificationContent).then(result => {
console.log("Notification sent!");
});
});
});
});
So when a new write operation is taking place as this path notifications/{uid}/userNotifications/{notificationId}
, whith other words a new document appears we can create a Promise
and send a notification.

- 130,605
- 17
- 163
- 193
-
I know the collection addition, I wanted to know and achive that whenever an entry is added using Andriod or NodeJs Or Angular A function may trigger and I could achieve another functionality, Like Adding a user's profile may generate a random Id for it. – DareDevil May 29 '18 at 18:15
-
I have asked you if you want to achieve that using the Firebase Console and you said yes. But according to your comment, please see my updated answer. – Alex Mamo May 29 '18 at 18:26
-
Yes I replied you Yes, because this was related to Firesase Cloud but using the function we need to achieve, obviously through console. – DareDevil May 29 '18 at 18:43
-
-