我在firestore中有两个功能:增加/减少对产品计数的int。下面的代码可以正常工作,但不是原子的,在快速增加/减少期间,等等...该值不可靠。 实际上,在恢复数据时,要增加数据然后进行更新,则会插入另一个操作并创建交织。
exports.createobject = functions.firestore
.document('objects/{id}')
.onCreate((snap, context) => {
var test = db.collection('system').doc('info');
test.get().then(function (doc) {
var currentTotalobjects = doc.data().totalobjects;
var toSaveTotal = currentTotalobjects + 1;
return test.update({
totalobjects: toSaveTotal
})
.then(function () {
console.log("Update totalobject during oncreate OK");
})
.catch(function (error) {
// The document probably doesn't exist.
console.error("Error during update count total_object: ", error);
});
}).catch(function (error) {
console.log("Error getting document:", error);
});
});
您知道如何维护原子操作吗?