簡體   English   中英

MongoError:寫沖突

[英]MongoError: WriteConflict

當我同時從前端( react )向后端( nodejs )發送多個請求時,服務器給我這個錯誤MongoError: WriteConflict 當我想更新數據庫中的一些數據時,我正在使用transaction

代碼:

const db = require("../../utils/database-connection/database");
const FollowersFollowing = require("../../models/followers-following/followers-following-models");

  const session = db.client().startSession();
  session.startTransaction();

      let whoFollowed;

      try {
        whoFollowed = await FollowersFollowing.updateFollow(
          userId,
          targetUserId,
          { session },
          "follow"
        );

        await Notice.updateNotice(
          userId,
          targetUserId,
          { session },
          "showUserOnTargetUserNotice"
        );

        await session.commitTransaction();
        session.endSession();
      } catch (error) {
        console.log(error);
        await session.abortTransaction();

        return next(
          new HttpError("could not follow the user, please try again.", 500)
        );
      }

一切正常,但是當我想發送多個請求(僅用於測試)時,服務器會拋出此錯誤。

有關正確的使用模式,請參閱https://docs.mongodb.com/manual/core/transactions/ 你必須重試 withTransaction 助手為你做的那些錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM