首页 > 解决方案 > 当我想使用 mongoose session transaction 使用 mongoose 的 updateMany() 函数时出现此错误

问题描述

错误 [ERR_UNHANDLED_ERROR]:未处理的错误。(null) 我该如何解决这个问题?在 node.js 中使用事务进行复制后,我在 E:\dev\node js\chat project\node_modules\mongoose\lib\model.js:5067 的 Function.emit (events.js:304:17) 收到此错误: 15 在 processTicksAndRejections (internal/process/task_queues.js:75:11) { 代码:'ERR_UNHANDLED_ERROR',上下文:空

try {
        if (deleteOnBothSides) {
            await PvMessageModel.updateMany(
                {_id: {$in: messageIds}, from: actorId},
                {$set: {is_deleted_from: true, is_deleted_to: true}},
                (e, messages) => {
                    throw e
                }).session(session)
            await PvMessageModel.updateMany(
                {_id: {$in: messageIds}, to: actorId},
                {$set: {is_deleted_to: true}},
                (e, messages) => {
                    throw e
                }).session(session)
        } else {
            await PvMessageModel.updateMany(
                {_id: {$in: messageIds}, from: actorId},
                {$set: {is_deleted_from: true}},
                (e, messages) => {
                    throw e
                }).session(session)
            await PvMessageModel.updateMany(
                {_id: {$in: messageIds}, to: actorId},
                {$set: {is_deleted_to: true}},
                (e, messages) => {
                    throw e
                }).session(session)
        }

        await session.commitTransaction();

        socket.emit('privateChatDeleteMessageResponse', {
            actor_id: actorId,
            message_ids: messageIds,
        })

        if (deleteOnBothSides)
            client.get(otherSideId, (e, reply) => {
                if (e) {
                    ErrorHandler.saveError(e, 'Node.js', currentFileName, 'socket.on("privateChatDeleteMessageResponse")', 6);
                    throw e;
                }
                if (reply != null)
                    // emmit to another side of chat if message deleter equals to message sender and delete on both sides is true
                    io.to(reply).emit('privateChatDeleteMessageResponse', {
                        actor_id: actorId,
                        message_ids: messageIds,
                    })
            })

    } catch (e) {
        ErrorHandler.saveError(e, 'Node.js', currentFileName, 'socket.on("privateChatDeleteMessage")', 7);
    } finally {
        session.endSession();
    }

标签: mongodbmongoosetransactions

解决方案


推荐阅读