提交 bf99f0a9 编写于 作者: 雪洛's avatar 雪洛

docs: transaction

上级 2bd0f4af
...@@ -1501,6 +1501,7 @@ exports.main = async (event) => { ...@@ -1501,6 +1501,7 @@ exports.main = async (event) => {
const bbbRes = await transaction.collection('account').doc('bbb').get() const bbbRes = await transaction.collection('account').doc('bbb').get()
if (aaaRes.data && bbbRes.data) { if (aaaRes.data && bbbRes.data) {
try {
const updateAAARes = await transaction.collection('account').doc('aaa').update({ const updateAAARes = await transaction.collection('account').doc('aaa').update({
data: { data: {
amount: _.inc(-10) amount: _.inc(-10)
...@@ -1519,6 +1520,10 @@ exports.main = async (event) => { ...@@ -1519,6 +1520,10 @@ exports.main = async (event) => {
return { return {
aaaAccount: aaaRes.data.amount - 10, aaaAccount: aaaRes.data.amount - 10,
} }
} catch(e) {
// 会作为 runTransaction reject 的结果出去
await transaction.rollback(-100)
}
} else { } else {
// 会作为 runTransaction reject 的结果出去 // 会作为 runTransaction reject 的结果出去
await transaction.rollback(-100) await transaction.rollback(-100)
...@@ -1568,8 +1573,8 @@ const db = uniCloud.database() ...@@ -1568,8 +1573,8 @@ const db = uniCloud.database()
const _ = db.command const _ = db.command
exports.main = async (event) => { exports.main = async (event) => {
try {
const transaction = await db.startTransaction() const transaction = await db.startTransaction()
try {
const aaaRes = await transaction.collection('account').doc('aaa').get() const aaaRes = await transaction.collection('account').doc('aaa').get()
const bbbRes = await transaction.collection('account').doc('bbb').get() const bbbRes = await transaction.collection('account').doc('bbb').get()
...@@ -1596,7 +1601,6 @@ exports.main = async (event) => { ...@@ -1596,7 +1601,6 @@ exports.main = async (event) => {
aaaAccount: aaaRes.data.amount - 10, aaaAccount: aaaRes.data.amount - 10,
} }
} else { } else {
await transaction.rollback()
return { return {
success: false, success: false,
...@@ -1605,6 +1609,7 @@ exports.main = async (event) => { ...@@ -1605,6 +1609,7 @@ exports.main = async (event) => {
} }
} }
} catch (e) { } catch (e) {
await transaction.rollback()
console.error(`transaction error`, e) console.error(`transaction error`, e)
return { return {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册