返回错误之后 userUpdate[users.SCHEMA.balance] = _.inc(-course.data[courses.SCHEMA.price]) 这段操作依旧被执行了
云数据库事务错误之后数据依然被写入https://developers.weixin.qq.com/miniprogram/dev/framework {errMsg: "cloud.callFunction:ok", result: {…}, requestID: "271c172f-64c8-48f2-9be7-2ecd8d83148a"}errMsg: "cloud.callFunction:ok"requestID: "271c172f-64c8-48f2-9be7-2ecd8d83148a"result: {code: "DATABASE_TRANSACTION_FAIL", message: "[ResourceUnavailable.TransactionNotExist] Transact…request, but if the problem persists, contact us.", requestId: "1647234744878_0.30454557461508514_33572427-17f86d787aa_9e"}__proto__: Object代码 exports.enrollCourse = async (event) => { try { await db.runTransaction(async transaction => { let resQuery = {} resQuery[reservations.SCHEMA.userId] = event.userId resQuery[reservations.SCHEMA.slotId] = event.slotId let res = await transaction.collection(reservations.TABLE_NAME).where(resQuery).get() if (res.data.length != 0) { await transaction.rollback({ code: 412, message: "You've already enrolled" }) return } let userQuery = {} userQuery[users.SCHEMA.openId] = event.userId let userEntry = await transaction.collection(users.TABLE_NAME).where(userQuery).get() if (userEntry.data.length == 0) { await transaction.rollback({ code: 403, detail: { table: users.TABLE_NAME } }) return } let user = userEntry.data[0] let futures = [] futures.push(transaction.collection(slots.TABLE_NAME).doc(event.slotId).get()) futures.push(transaction.collection(courses.TABLE_NAME).doc(event.courseId).get()) let result = await Promise.all(futures) let slot = result[0] let course = result[1] if (!course.data) { await transaction.rollback({ code: 403, detail: { table: courses.TABLE_NAME } }) return } if (!slot.data) { await transaction.rollback({ code: 403, detail: { table: slots.TABLE_NAME } }) return } if (slot.data[slots.SCHEMA.enrolledNumber] >= course.data[courses.SCHEMA.capacity]) { await transaction.rollback({ code: 412, message: "Full capacity" }) return } if (slot.data[slots.SCHEMA.limitCourseId] != undefined && slot.data[slots.SCHEMA.limitCourseId] != course.data[courses.SCHEMA.id]) { await transaction.rollback({ code: 412, message: "Course id doesn't match" }) return } console.log(user[users.SCHEMA.balance], course.data[courses.SCHEMA.price]) if (user[users.SCHEMA.balance] < course.data[courses.SCHEMA.price]) { await transaction.rollback({ code: 412, message: "Insufficient Balance" }) return } let writes = [] userUpdate = {} userUpdate[users.SCHEMA.balance] = _.inc(-course.data[courses.SCHEMA.price]) writes.push(transaction.collection(users.TABLE_NAME).where(userQuery).update({ data: userUpdate })) slotUpdate = {} slotUpdate[slots.SCHEMA.enrolledNumber] = _.inc(1) slotUpdate[slots.SCHEMA.limitCourseId] = course.data[courses.SCHEMA.id] writes.push(transaction.collection(slots.TABLE_NAME).doc(event.slotId).update({ data: slotUpdate })) data = {} data[reservations.SCHEMA.userId] = event.userId data[reservations.SCHEMA.slotId] = event.slotId data[reservations.SCHEMA.startTs] = slot.data[slots.SCHEMA.startTs] data[reservations.SCHEMA.courseId] = event.courseId data[reservations.SCHEMA.courseName] = course.data[courses.SCHEMA.name] data[reservations.SCHEMA.price] = course.data[courses.SCHEMA.price] data[reservations.SCHEMA.anonymous] = !event.isPublic writes.push(transaction.collection(reservations.TABLE_NAME).add({ data: data })) try { await Promise.all(writes) } catch (e) { console.error(e) await transaction.rollback({ code: 500, message: e.errMsg, }) } }) } catch (e) { if (e.code) { return e } else { console.error("hehe", e) return { code: 500, message: e.errMsg, } } } return { code: 200, } }
2022-03-14