Why is this Realm write function throwing "realm is already in a write transaction" error?

305 Views Asked by At

In a react-native app, using realm.js, the below function is throwing The realm is already in a write transaction error.

function definition:

export const addJob = (params) => {
  const job = {
    ...params,
    id: uuid.v4(),
    account: realm.objectForPrimaryKey('Account', params.accountId),
    product: realm.objectForPrimaryKey(
      'Product',
      params.accountId + '_' + params.productId
    ),
    dateAdded: Date.now(),
  };
  let Job;
  realm.write(() => {
    Job = realm.create('UploadQueueJob', job);
  });
  return Job;
};

function invocation:

export const invocation = async (task, tasks) => {
  const sessionId = task.sessionId;
  const account = task.account;

  const job = await addJob({
    accountId: task.account.accountId,
    productId: task.product.productId,
    status: 1,
    dateCompleted: 0,
    photosRemoved: false,
  });
  await updateProduct({
    accountId: task.account.accountId,
    productId: task.product.productId,
  });
}

Why is this error being thrown?

0

There are 0 best solutions below