I have seen an increment counter with Cloud Functions referencing Realtime Database, but not Firebase Firestore yet.
I have a cloud function that listens for new documents:
exports.addToChainCount = functions.firestore
.document('chains/{name}')
.onCreate((snap, context) => {
// Initialize document
var chainCounterRef = db.collection('counters').doc('chains');
var transaction = db.runTransaction(t => {
return t.get(chainCounterRef).then(doc => {
// Add to the chain count
var newCount = doc.data().count + 1;
t.update(chainCounterRef, { count: newCount });
});
}).then(result => {
console.log('Transaction success!');
}).catch(err => {
console.log('Transaction failure:', err);
});
return true;
});
I'm attempting the above transaction, but when I run firebase deploy
in terminal I get this error:
error Each then() should return a value or throw promise/always-return functions predeploy error: Command terminated with non-zero exit code1
This is my first attempt at anything node.js, and I'm not sure I've written this right.
Firestore now has a specific operator for this called FieldValue. increment() . By applying this operator to a field, the value of that field can be incremented (or decremented) as a single operation on the server.
Lastly, it'll scale massively and automatically. Firebase Functions will just do it automatically for you based on the traffic you receive and at an incredibly low cost.
Firebase Webhooks automates the workflow for companies and Developers by triggering the defined events via URL. Firebase Webhooks Integration is the simplest and most efficient way to communicate between app and Firebase.
Let’s take a look at how it works… Increment is a special value that will atomically update a counter based on the interval you specify. You do not need to read the document to obtain the current total - Firebase does all the calculation serverside. In most cases, you will be keeping a count by adding an integer of 1 to the current total.
Many realtime apps have documents that act as counters. For example, you might count 'likes' on a post, or 'favorites' of a specific item. In Cloud Firestore, you can only update a single document about once per second, which might be too low for some high-traffic applications.
You do not need to read the document to obtain the current total - Firebase does all the calculation serverside. In most cases, you will be keeping a count by adding an integer of 1 to the current total. We can decrement a counter by simply changing the interval value to -1.
Tune in to learn how Firebase can help you accelerate app development, release with confidence, and scale with ease. Register Many realtime apps have documents that act as counters. For example, you might count 'likes' on a post, or 'favorites' of a specific item.
There is now a much simpler way to increment/decrement a field in a document: FieldValue.increment()
. Your sample would be something like this:
const FieldValue = require('firebase-admin').firestore.FieldValue;
var chainCounterRef = db.collection('counters').doc('chains');
chainCounterRef.update({ count: FieldValue.increment(1) });
See:
If you want to safely increment a number in a document, you can use a transaction. The following code is taken directly from the linked page. It adds one to a field called population
in a document /cities/SF
after giving it some initial values:
// Initialize document
var cityRef = db.collection('cities').doc('SF');
var setCity = cityRef.set({
name: 'San Francisco',
state: 'CA',
country: 'USA',
capital: false,
population: 860000
});
var transaction = db.runTransaction(t => {
return t.get(cityRef)
.then(doc => {
// Add one person to the city population
var newPopulation = doc.data().population + 1;
t.update(cityRef, { population: newPopulation });
});
}).then(result => {
console.log('Transaction success!');
}).catch(err => {
console.log('Transaction failure:', err);
});
Bear in mind that Firestore is limited to one write per second under sustained load, so if you're going to be writing a lot, you will need to use a sharded counter instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With