Have a nice day. First; I would like to thank this user. https://stackoverflow.com/a/52982781/11320646 I want you to know that I'm new to writing code. I have to train a project. The function of these codes(include link, "solution" tittle) is this:
customers/{id}
counts the consumers here and write count here:
metadatas/customers, {count:123456}
.
But i want to add counter here:
groups/{groupID}/posts/{postID}
and ı should write count here:
metadatas/{groupID}/counts, {count:123456}
But these codes write:
metadatas/posts, {count:123456}
How to add the groupID
layer?
I've tried many things, but I haven't succeeded.
I have successfully done counting with these codes, I have activated the retry option if it fails. I got a great result. At the same time more than 100 post was added and the result is excellent. my codes are here:
const executeOnce = (change, context, task) => {
const eventRef = firestore.collection('events').doc(context.eventId);
return firestore.runTransaction(t =>
t
.get(eventRef)
.then(docSnap => (docSnap.exists ? null : task(t)))
.then(() => t.set(eventRef, { processed: true }))
);
};
const documentCounter = collectionName => (change, context) =>
executeOnce(change, context, t => {
// on create
if (!change.before.exists && change.after.exists) {
return t
.get(firestore.collection('counts')
.doc(collectionName))
.then(docSnap =>
t.set(docSnap.ref, {
count: ((docSnap.data() && docSnap.data().count) || 0) + 1
}));
// on delete
} else if (change.before.exists && !change.after.exists) {
return t
.get(firestore.collection('counts')
.doc(collectionName))
.then(docSnap =>
t.set(docSnap.ref, {
count: docSnap.data().count - 1
}));
}
return null;
});
exports.customersCounter = functions.firestore
.document('denemeicerikler/{grupID}/icerikler/{postID}')
.onWrite(documentCounter('icerikler'));