简体   繁体   中英

MongoDB Transactions - Converting circular structure to JSON error

For implementing transactions in mongoose or mongodb, do we need replica set? I am trying to implement transaction in mongodb using the below code

    const session = await mongoose.startSession();
      await session.startTransaction();
      try {
        // Writing to Invalid DB
        if (invalidData && invalidData.length) {
          const transformedData = this.transform(invalidData);
          // console.log('transformedData---------->', transformedData);
          const invalidOutput = await InvalidDataModel.insertMany([transformedData], { session });
        await session.commitTransaction();
        session.endSession();
      } catch (error) {
        console.log({ err: error.errmsg, result: error.result.result.writeErrors });
        await session.abortTransaction();
        session.endSession();
        throw error;
      }

But I am getting the error

022-07-19T10:05:00.940Z - error: uncaughtException: Converting circular structure to JSON
    --> starting at object with constructor 'Server'
    |     property 's' -> object with constructor 'Object'
    |     property 'coreTopology' -> object with constructor 'Server'
    |     ...
    |     property 's' -> object with constructor 'Object'
    --- property 'topology' closes the circle date=Tue Jul 19 2022 15:35:00 GMT+0530 (India Standard Time), pid=50111, uid=503, gid=20,

Need help with the issue?

This was happening because there was a JSON.stringify(query) due to which this circular error was happening. Removed it. Use flatted library and the error is not coming.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM