【发布时间】:2018-07-29 05:25:21
【问题描述】:
问题
我有一个 Cloud 函数,它根据文档 state 字段中的更改更新 Cloud Firestore 文档。这些更新必须以特定顺序发生。当快速连续进行两项更改时,无法保证 Cloud Functions 将以正确的顺序运行。有没有办法让 Cloud Firestore 事务重试直到成功或超时?
-
Document.state设置为stage1 -
Document.state更新为stage2 -
Document.state更新为stage3 - 云函数被触发并读取
stage3 - 云函数被触发并读取
stage2
在 Cloud Functions 文档中,它讨论了在失败时重试事务的能力。但是,此选项在 GCP 控制台的 Cloud Functions 部分中显示为灰色(未显示在 Firebase 控制台中)
示例代码
传入的变量
myDocumentRef: db.doc('myCollection/myDocument')
newState: stage3
交易代码
var transaction = db.runTransaction(t => {
return t.get(myDocumentRef)
.then(doc => {
if ((newState = 'stage2' && doc.data().state = 'stage1') ||
(newState = 'stage3' && doc.data().state = 'stage2')) {
t.update(myDocumentRef, { population: newPopulation });
} else {
// Keep retrying the transaction until it succeeds
}
});
}).then(result => {
console.log('Transaction success!');
}).catch(err => {
console.log('Transaction failure:', err);
});
【问题讨论】:
标签: javascript firebase google-cloud-functions google-cloud-firestore