How to read the latest document created in Firestore using node?
Firstly, in an onCreate handler, you can access the id of just the newly created document by through the snap object, you can access the id via snapshot.id
and the body via snapshot.data()
.
You already have the newly created document fetched, so no need to fetch entire collection. You should replace this entire section:
const deviceIdTokens = await admin
.firestore()
.collection('FCM')
.get();
var tokens = [];
var i=0;
for (var token of deviceIdTokens.docs) {
tokens.push(token.data().ar1[i]);
i++;
}
with this:
const tokens = snapshot.data().ar1;
UPDATE: To delete the new document, you could do
await firestore().collection("FCM").doc(snapshot.id).delete();
since the new document belongs in the FCM
collection, not "helpReqs"
Comments
-
Madhavam Shahi over 1 year
I wrote a cloud function, to listen for document creation in a collection, in my database
here is the function,
const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(functions.config().functions); var newData; exports.myTrigger = functions.firestore.document('FCM/{id}').onCreate(async (snapshot, context) => { // if (snapshot.empty) { console.log('No Devices'); return; } newData = 'hello'; const deviceIdTokens = await admin .firestore() .collection('FCM') .get(); var tokens = []; var i=0; for (var token of deviceIdTokens.docs) { tokens.push(token.data().ar1[i]); i++; } var payload = { notification: { title: 'push title', body: 'push body', sound: 'default', }, data: { push_key: 'Push Key Value', key1: newData, }, }; try { const response = await admin.messaging().sendToDevice(tokens, payload); console.log('Notification sent successfully'); } catch (err) { console.log(err); } });
This function works weirdly,
For example, sometimes it sends notification, and sometimes it does not.
I don't know how to resolve this issue,
In my arr1 field, i have an array of device tokens, to whom i want to send notifications to,
i want the function to send notifications only to the devices(using tokens) which are just created(in the newly created document ),then delete the document.
I think it's sending notifications to all the documents at once.
I'm pretty new at node..
Is there anyway to only send notifications to the device tokens present in only one document (..the latest created document)?.. I think it's sending notifications to all.
please help me out.
UPDATE:- Here is my document structure
-
Madhavam Shahi over 3 yearsThanks, it worked! i had onemore question, how can i delete that newly created document, i tried this code const res = await admin.firestore.collection('helpReqs').doc(snapshot.id).delete();, but it didn't work.
-
Madhavam Shahi over 3 yearsAnd deleting the document after sending the notification is a good practice? i mean, will it increase my bill amount?
-
JayCodist over 3 yearsYes, @MadhavamShahi, you are charged for extra document deletes (a very small charge actually), but that's no reason to not delete a document when you want to and it's OK as a practice to do just that. I've edited my answer to show how you can delete the new document
-
JayCodist over 3 yearsThe only question is whether, the document is no longer useful. I would suggest that if the only purpose of creating the documents in
FCM
is to use to tokens inar1
for notifications, then no need to delete, just keep accessing the newly created doc. It's cheaper that way -
JayCodist over 3 yearsAlso, if my solution answers you question, you should consider "accepting" it as the answer. :-)
-
Madhavam Shahi over 3 yearsHey, thanks for explaining, i understood everything, :), and yeah, accepting it(..i forgot earlier ^_^' ) thanks again dude.