Back

[SOLVED] Functions: Unable to write to Collections

  • 0
  • Functions
Francisco "Klogan" Barros
4 Jan, 2024, 22:52

I just created a serverless function on my a Appwrite project, pointing at a source repository (no templates).

I am authenticated as the owner of the project. Trying to run the function from Appwrite Cloud Console UI and getting this error:

TypeScript
Error: app.64f85c5ce3ecabddd466@service.cloud.appwrite.io (role: applications) missing scope (collections.read)
    at Client.call (/usr/local/server/src/function/node_modules/node-appwrite/lib/client.js:172:31)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Databases.getCollection (/usr/local/server/src/function/node_modules/node-appwrite/lib/services/databases.js:267:16)
    at async createMigrationCollection (file:///usr/local/server/src/function/node_modules/@franciscokloganb/appwrite-database-migration-tool/dist/lib.esm.js:599:22)
    at async Module.default (file:///usr/local/server/src/function/functions/database/migrations-create-collection.js:4:3)
    at async execute (/usr/local/server/src/server.js:141:22)
    at async /usr/local/server/src/server.js:158:13

The function tries to create a collection. How do I add the missing scope to anyone who belongs to the project?

Note: I have global appwrite variables set on the project and the function picks them up correctly. Isn't Appwrite Server SDK supposed to be able to do everything everywhere, why is it missing scopes?

TL;DR
The user is facing an issue while trying to create a collection in an Appwrite project using a serverless function. The error message indicates that the function is missing the `collections.read` scope. The user is looking for guidance on how to add this scope to their project. Solution: To add the `collections.read` scope to the project, follow these steps: 1. Go to your Appwrite project in the Appwrite Cloud Console UI. 2. Navigate to the "API Keys" section. 3. Find the API key that you are using in your serverless function. 4. Click on the "Edit" button next to the
Francisco "Klogan" Barros
4 Jan, 2024, 22:53
TypeScript
import { Client, Databases } from 'node-appwrite';
import invariant from 'tiny-invariant';

import { MIGRATIONS_COLLECTION_ID, MIGRATIONS_COLLECTION_NAME } from './constants';
import type { Logger } from './types';

function configuration() {
  // The function in the repository executes invariant, ensuring all envs are defined.
  // I am also seeing the correct values in the logs.
  const apiKey = process.env['APPWRITE_API_KEY'];
  const collectionId = process.env['MIGRATIONS_COLLECTION_ID'];
  const collectionName = process.env['MIGRATIONS_COLLECTION_NAME'];
  const databaseId = process.env['MIGRATIONS_DATABASE_ID'];
  const endpoint = process.env['APPWRITE_ENDPOINT'];
  const projectId = process.env['APPWRITE_FUNCTION_PROJECT_ID'];

  return {
    apiKey,
    collectionId,
    collectionName,
    databaseId,
    endpoint,
    projectId,
  };
}

export async function createMigrationCollection({ log, error }: { log: Logger; error: Logger }) {
  log('Create migration collection started.');

  const { endpoint, apiKey, databaseId, collectionId, collectionName, projectId } = configuration();

  log(`Initiating client. Endpoint: ${endpoint}, ProjectID: ${projectId}`);

  const client = new Client().setEndpoint(endpoint).setProject(projectId).setKey(apiKey);
  const databaseService = new Databases(client);
  const collection = await databaseService.getCollection(databaseId, collectionId);

  if (collection) {
    log('Create migration collection exited. Collection already exists.');

    return;
  }

  await databaseService
    .createCollection(databaseId, collectionId, collectionName)
    .then(() => log('Create migration collection completed successfully.'))
    .catch((e) => {
      error(`Could not create collection ${collectionName} (id: ${collectionId}).`);

      if (e instanceof Error) {
        error(e.message);
      }
    });
}
ideclon
4 Jan, 2024, 23:00

Have you added the collections.read scope to your API key?

Francisco "Klogan" Barros
4 Jan, 2024, 23:01

Indeed. πŸ₯Ή I did not.

Francisco "Klogan" Barros
4 Jan, 2024, 23:01

I just noticed I had a few - signs on my Scopes

Francisco "Klogan" Barros
4 Jan, 2024, 23:01

I will try rerunning.

Francisco "Klogan" Barros
4 Jan, 2024, 23:13

βœ…

D5
4 Jan, 2024, 23:14

Can I mark this as solved?

Francisco "Klogan" Barros
4 Jan, 2024, 23:36

Yes, thanks

D5
4 Jan, 2024, 23:37

[SOLVED] Functions: Unable to write to Collections

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