Unable update a specific field in realm database

I’m trying to query an object change its value to false e.g isDefault: false after creating a new business profile. This is how the function looks like:

const addHandler = async () => {
    try {
      //first create the realm object
      realm.write(() => {
        realm.create(
          'BusinessEntity',
          TBusiness.generate(
            business.accountingPeriodId!,
            business.name!,
            business.registrationNumber!,
            business.vatRegistered!,
            business.vatNumber!,
            business.startDate!,
            business.endDate!,
            business.streetAddress!,
            business.suburb!,
            business.province!,
            business.postalCode!,
            business.autoGenerateCustomerCode!,
            business.autoGenerateItemCode!,
            business.invoiceMessage!,
            business.quotationMessage!,
            business.bankName!,
            business.branchCode!,
            business.accountNumber!,
            business.accountName!,
            true,
          ),
        );
      });

      let newBusiness = await query.find(x => x.name === business.name);
      let businessObject = realm.objects(TBusiness);

      dispatch(addBusinessAction(newBusiness));

      //update state, set previous to false
      if (businessArray.businesses!.length > 0) {
        //update any business entity that was created to "false"
        businessObject.findIndex(x => {
          if (x.name !== business.name) {
            x.isDefault = false;
          }
        });
      }

      setBusiness({});
    } catch (err) {
      console.log(err);
      throw err;
    }
  };

When the database is empty I’m able to create a business profile with no errors, but when I create a second business profile and try to change the previous profile field isDefault to false I get the following errors:

 LOG  [Error: Wrong transactional state (no active transaction, wrong type of transaction, or transaction already in progress)
Exception backtrace:
<backtrace not supported on this platform>]
 ERROR  Error: Wrong transactional state (no active transaction, wrong type of transaction, or transaction already in progress)
Exception backtrace:
<backtrace not supported on this platform>

Hi Tony.

I’m an engineer on the Realm SDK team for Realm JavaScript and I’m happy to help you resolve this. Sorry for the wait on our part. I see that the x.isDefault = false; is happening outside of the callback passed to realm.write and I wonder what happens if you wrap it in a realm.write like so:

realm.write(() => { x.isDefault = false; });

I also see that the entire function is async because you’re awaiting the result of query.find. However I would not expect that to return a promise, can you remove that await and the async in the implementation of your addHandler arrow function? I think that could help simplify the function and lower the chance of concurrency issues.

On a slightly different note: We’ve seen similar issues (related to transaction states) for users of the @realm/react package - are you using that by any chance?

3 Likes

Yes, I am using @realm/react package. By the way, is the package not ready for production yet?

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.