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.