收藏
回答

云数据库事务错误之后数据依然被写入

框架类型 问题类型 终端类型 AppID 环境ID 基础库版本
小程序 Bug 工具 wx846a6c34278d75d6 cloud1-5g3heh176335048c 2.14.1

https://developers.weixin.qq.com/miniprogram/dev/framework

  1. {errMsg: "cloud.callFunction:ok", result: {…}, requestID: "271c172f-64c8-48f2-9be7-2ecd8d83148a"}
  2. errMsg"cloud.callFunction:ok"
  3. requestID"271c172f-64c8-48f2-9be7-2ecd8d83148a"
  4. result: {code"DATABASE_TRANSACTION_FAIL"message"[ResourceUnavailable.TransactionNotExist] Transact…request, but if the problem persists, contact us."requestId"1647234744878_0.30454557461508514_33572427-17f86d787aa_9e"}
  5. __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: {
                        tableusers.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: {
                        tablecourses.TABLE_NAME
                    }
                })
                return
            }
            if (!slot.data) {
                await transaction.rollback({
                    code: 403,
                    detail: {
                        tableslots.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({
                datadata
            }))
            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,
    }
}
回答关注问题邀请回答
收藏

1 个回答

  • 魏诚
    魏诚
    2022-03-14

    返回错误之后

    userUpdate[users.SCHEMA.balance] = _.inc(-course.data[courses.SCHEMA.price])
    


    这段操作依旧被执行了

    2022-03-14
    有用
    回复
登录 后发表内容
问题标签