【发布时间】:2021-09-28 06:00:47
【问题描述】:
我正在开发一个使用 Firestore 存储用户数据的应用程序。
据我们所知,写入 Firestore 文档的数据不是实时的,当写入频率较高时会导致问题。
所以我打算在 Firestore 中使用 runTransaction 功能。
根据here 给出的官方文档,似乎使用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; 的输出是什么?
这是否提供尚未提交的最新更新?还是给出之前提交的数据?
感谢您的帮助。
谢谢
【问题讨论】:
标签: javascript node.js firebase google-cloud-firestore