0

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?

Jim
  • 1,988
  • 6
  • 34
  • 68
  • Can you add additional troubleshooting and indicate what line in the code is throwing that error? – Jay Jan 15 '22 at 14:05
  • @jay error doesn’t indicate a line it only indicates the “addJob” function – Jim Jan 16 '22 at 15:51
  • You need to do further troubleshooting. Best practice is to step through your code, line by line, inspecting the vars and code execution along the way. When you discover which line is not correct, update your question with that info. You may also want to add some print type statements to output vars to console so you can also see what the code itself is seeing. – Jay Jan 16 '22 at 16:11

0 Answers0