我曾经看到过一个使用云函数引用实时数据库的增量计数器,但还没有针对Firebase Firestore。
我有一个云函数监听新文档:
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;
});
我正在尝试以上交易,但是当我在终端中运行firebase deploy
时,出现以下错误:
error Each then() should return a value or throw promise/always-return functions predeploy error: Command terminated with non-zero exit code1
这是我第一次尝试使用node.js,我不确定我是否写得正确。