Skip to content
Back

Function does not have createDocument access

  • 0
  • Databases
  • Functions
  • Auth
  • Cloud
AngryAnt
23 Sep, 2025, 08:53

I have a function which I'm trying to have create a document in a collection. The collection in question has row security enabled and permissions set to grant users create access.

The purpose of the function is to clone a bunch of documents for the user, including ones in this particular collection. The function implementation is as follows:

TypeScript
async function cloneAnswerAsync (baseAnswer, userID, createdAnswerFiles)
{
  try
  {
    let clonedAnswerFileID = null;

    // If the answer has a file, clone it.
    if (baseAnswer["value-file"])
    {
      clonedAnswerFileID = await cloneAnswerFileAsync (baseAnswer["value-file"], userID);
      if (!clonedAnswerFileID)
      {
        return null;
      }
      createdAnswerFiles.push (clonedAnswerFileID);
    }

    // Create the new Answer with cloned data.
    const clonedData = { ...baseAnswer };
    
    // Remove system fields.
    delete clonedData.$id;
    delete clonedData.$createdAt;
    delete clonedData.$updatedAt;
    delete clonedData.$permissions;
    delete clonedData.$collectionId;
    delete clonedData.$databaseId;

    // Override file reference if cloned.
    if (clonedAnswerFileID)
    {
      clonedData["value-file"] = clonedAnswerFileID;
    }

    // TODO: Figure out why this results in AppwriteException: The current user is not authorized to perform the requested action.
    const result = await databases.createDocument (
      databaseID,
      collectionAnswers,
      ID.unique (),
      clonedData,
      [`write("user:${userID}")`, `read("user:${userID}")`]
    );

    return result;
  }
  catch (exception)
  {
    error ("Failed to clone answer: ", exception);
    return null;
  }
}

The function scopes are set to: users.read, rows.read, rows.write, execution.write and executes its API calls via a Client configured with an API key given via env var, configured with the same scopes.

Any ideas what might be going on here?

TL;DR
Function `cloneAnswerAsync` is meant to clone documents in a collection for a user, but encounters an AppwriteException stating the current user is not authorized to perform the action. The issue could be related to the permissions settings in the collection where the function is trying to create documents. Check if the user executing the function has proper permissions to create documents in the collection.
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more