Recursive UDF needs explicit type

I’m trying to write a UDF as:

function function_delete_transaction(transaction_id, transfer_deleted) {
  let transaction = Collection("collection_transactions").byId(transaction_id)
  let transaction_account = transaction.account // document reference
  let transaction_user = transaction.user // document reference
  /*
    transaction.transfer is a document reference, but it would only exist
    if the transaction type was 'transfer' (that is, transferred from one
    account to another).
  */
  if (transaction.transfer != null && transfer_deleted != true) {
    function_delete_transaction(transaction.transfer.id, true)
  }
  transaction_account.updateData({
    balance: transaction_account.balance - transaction.amount,
    transaction_count: transaction_account.transaction_count - 1
  })
  transaction_user.updateData({
    net_worth: transaction_user.net_worth - transaction.amount,
    transaction_count: transaction_user.transaction_count - 1
  })
  transaction.delete()
}

I get an error saying “Error updating schema” with the following details:

Error: Invalid database schema update.
    error: Recursively defined `function_delete_transaction` needs explicit type.
    at *function_delete_transaction.body*:1:1
       |
     1 |   (transaction_id, transfer_deleted) => {
       |  _^

I believe the return type here would be null as that’s what .delete() returns for a document. But, how do I specify that type for the function to get rid of this error?

Solved! It should be defined as:

function function_delete_transaction(transaction_id: ID, transfer_deleted: Boolean): NullCollectionDef {
  let transaction = Collection("collection_transactions").byId(transaction_id)
  let transaction_account = transaction.account
  let transaction_user = transaction.user
  if (transaction.transfer != null && transfer_deleted != true) {
    function_delete_transaction(transaction.transfer.id, true)
  }
  transaction_account.updateData({
    balance: transaction_account.balance - transaction.amount,
    transaction_count: transaction_account.transaction_count - 1
  })
  transaction_user.updateData({
    net_worth: transaction_user.net_worth - transaction.amount,
    transaction_count: transaction_user.transaction_count - 1
  })
  transaction.delete()
}

Found it from here. Only when I ran the example command in the shell, and inspected the generated UDF, that’s when I figured how to write types in Fauna (I was adding an extra space).

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