[英]runTransaction: Provided document reference is from a different Firestore instance error
[英]runTransaction in Firestore
我正在開發一個使用 Firestore 來存儲用戶數據的應用程序。
正如我們所知,寫入 Firestore 文檔的數據不是實時的,當寫入頻率較高時會導致問題。
所以我打算在 Firestore 中使用runTransaction
功能。
根據此處給出的官方文檔,使用runTransaction
似乎有助於處理這種並發寫入。
以下是官方文檔中給出的示例的修改版本:
// Initialize document
const studentRef = db.collection('Student').doc('stud1');
await studentRef.set({
name: 'New Name',
id: 'stud11X',
present: false,
});
UpdateStudentData: async function () {
try {
await db.runTransaction(async (t) => {
const doc = await t.get(studentRef);
const isPresent = doc.data().present;
t.update(studentRef, {population: true});
});
console.log('Transaction success!');
} catch (e) {
console.log('Transaction failure:', e);
}
}
我的問題是,例如,如果我們嘗試在數據已經在進行中並且尚未提交時更新數據,那么const isPresent = doc.data().present;
的輸出是什么const isPresent = doc.data().present;
?
這是否提供了尚未提交的最新更新? 或者它是否提供了以前提交的數據?
感謝您的幫助。
謝謝
您將在事務中獲得的數據是數據庫中的最新數據。 那些保存在客戶端且未提交到數據庫的內容將不會出現在事務中。 事務確保每個路徑只能寫入一次,因此如果存在從客戶端將數據提交到數據庫的過程,您的事務將自動重試,直到下一個寫入數據庫。 很難判斷是您的交易還是來自其他設備的交易會是第一個。 這就是我們進行交易的原因。 和他們在一起應該沒關系。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.