MongoDB Transactions - Converting circular structure to JSON error

Viewed 24

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?

1 Answers

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.

Related