写操作后如何通过读操作实现Firebase事务 [英] How to implement Firebase transactions with read operations after write operations
问题描述
有没有一种方法可以在nodejs中实现针对云和后台功能的写入操作之后的读取操作?
Is there a way to implement read operations after write operations in nodejs for cloud and background functions?
docs 中所述,仅服务器客户端库在写操作之后通过读操作来支持事务.但是我不能实现一种不会触发错误的方法:
As stated in docs, only server client libraries support transactions with read operations after write operations. But I can't implement one to not trigger error:
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"
I'm using "firebase-admin": "^9.6.0"
that uses @google-cloud/firestore "4.5.0"
and
"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'] });
}
};
推荐答案
Docs 说错了,服务器库不支持写后读取.文档将很快修复.
Docs stated wrong and server libraries doesn't support reads after writes. Documentation will be fixed soon.
从代码的角度来看,没有办法克服这种情况.
From the point of code, there's no way to overcome such situation.
这篇关于写操作后如何通过读操作实现Firebase事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!