node.js - 写操作后如何使用读操作实现 Firebase 事务
问题描述
有没有办法在nodejs中为云和后台功能实现写操作后的读操作?
如docs中所述,只有服务器客户端库支持在写操作后进行读操作的事务。但我无法实现一个不触发错误:
Error: Firestore transactions require all reads to be executed before all writes.
at Transaction.get (/workspace/node_modules/@google-cloud/firestore/build/src/transaction.js:76:19)
at /workspace/lib/firestore/tests-write.js:46:11
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async Transaction.runTransaction (/workspace/node_modules/@google-cloud/firestore/build/src/transaction.js:323:26)
at async /workspace/lib/firestore/tests-write.js:43:5
我正在使用"firebase-admin": "^9.6.0"
那个用途@google-cloud/firestore "4.5.0"
和
"firebase-functions": "^3.13.2"
后台功能(触发器):
const onFirestoreTestsWrite = functions.firestore
.document('tests/{testId}')
.onWrite(async (change, context) => {
await admin.firestore().runTransaction(async (t) => {
const testDoc = await admin.firestore().collection('tests').doc().get();
t.set(testDoc.ref, {});
t.get(testDoc.ref);
});
});
云功能(http):
const tests_get = async (req, res) => {
try {
const test = await admin.firestore().runTransaction(async (t) => {
const testDoc = await admin.firestore().collection('tests').doc().get();
t.set(testDoc.ref, {});
return t.get(testDoc.ref);
});
res.send({id: test.id});
} catch (e) {
res.status(400).send({ error: e['message'] });
}
};
解决方案
文档说明错误,服务器库不支持先读后写。文档将很快修复。
从代码的角度来看,没有办法克服这种情况。
推荐阅读
- python - 使用 python-docx 时出现错误“找不到参考‘文档’”
- python - 我想让这个 age_alarm 函数在我输入 str 时不返回任何内容
- javascript - 尝试使用 Date() 和 moment.js 获取正确日期的问题
- python - 找不到按钮 python selenium
- opentext - 简单 SQLTalk 的替代品
- xml - 西门子开放 XML 文件
- sql - SQL 聚合函数的数据类型(COUNT、SUM、AVG)
- git - 从已在本地删除的旧提交中删除已提交的文件
- clang - 有没有办法在没有 sudo 的情况下在 centOS 上获取 llvm/clang?
- vue.js - 未知的自定义元素:
使用 vuetify 组件时