我有一个事务,其中包含一个更新操作和一个设置操作。从客户端,我向事务发送2个并发请求。即使设置未成功,更新操作也会发生多次。
public addChatMessage(msg, callback, errorCallBack) {
let msgColl = this.admin.firestore().collection('messages');
var obj1 = JSON.parse(msg);
var uid1 = obj1.message.senderID + "_"+obj1.message.senderLocalPostNo;
this.admin.firestore().collection('messages').doc(uid1).get().then((docSnapshot) => {
if (!docSnapshot.exists) {
let metaRef = this.admin.firestore().doc('messages/msgsMeta');
let msgRef = msgColl.doc(uid1);
let transaction = this.admin.firestore().runTransaction(t => {
return t.get(msgRef).then(a => {
return t.get(metaRef)
.then(doc => {
let newCounter = doc.data().counter + 1;
var finalResult = {message:obj1.message,serverPostNo:newCounter};
t.update(metaRef, {counter: newCounter});
t.set(msgRef, finalResult);
});
});
}).then(result => {
console.log('Transaction success', result);
}).catch(err => {
console.log('Transaction failure:', err);
});
} else {
console.log("document already exists");
}
});
console.log("EOF addChatMessage");
}
我希望如果无法进行设置操作,更新操作将回滚,但不会回滚。