Back

[SOLVED] Cannot create document

  • 0
  • Tools
  • Databases
  • Web
  • Self Hosted
RangerDev
19 May, 2023, 15:35

Hello! I am trying to create a document but getting this error:

TypeScript
pwrite.209ebd24.js:1     POST https://cloud.appwrite.io/v1/databases/646538e35dd17306c589/collections/646538eeeface7a4cd39/documents 401
(anonymous) @ appwrite.209ebd24.js:1
F @ appwrite.209ebd24.js:1
(anonymous) @ appwrite.209ebd24.js:1
(anonymous) @ appwrite.209ebd24.js:1
p @ appwrite.209ebd24.js:1
call @ appwrite.209ebd24.js:1
(anonymous) @ appwrite.209ebd24.js:1
(anonymous) @ appwrite.209ebd24.js:1
p @ appwrite.209ebd24.js:1
createDocument @ appwrite.209ebd24.js:1
I @ create.f48802b9.js:1
create.f48802b9.js:1 AppwriteException: The current user is not authorized to perform the requested action.
    at Pe.<anonymous> (https://pitchfork.vercel.app/_astro/appwrite.209ebd24.js:1:19652)
    at Generator.next (<anonymous>)
    at c (https://pitchfork.vercel.app/_astro/appwrite.209ebd24.js:1:14540)
TL;DR
Issue: The user is receiving a 401 error when trying to create a document using the Appwrite API. The error message states that the current user is not authorized to perform the requested action. Solution: 1. Verify that write permissions have been added for the `646538eeeface7a4cd39` collection in the collection settings. 2. Remove the following permission lines from the code: - Permission.delete(Role.team("Owner")) - Permission.update(Role.team("Owner")) - Permission.read(Role.team("Owner")) - Permission.delete(Role.team("Admin")) - Permission.update(Role.team("Admin"))
Binyamin
19 May, 2023, 15:36

Have you add write permissions for the 646538eeeface7a4cd39 collection?

RangerDev
19 May, 2023, 15:36

Here is the code:

TypeScript
<script lang="ts">
import { appwriteStorage,appwriteDatabases,appwriteUser } from "../lib/appwrite";

import { ID , Role, Permission } from "appwrite";

export let userID = '';

let IconID = ID.unique();
let ThumbnailID = ID.unique();
let IconUpload  = document.getElementById('IconFile') as HTMLInputElement;
let ThumbnailUpload  = document.getElementById('ThumbnailFile') as HTMLInputElement;


let PostTitle = '';
let PostTagline = '';
let PostDescription = '';
let PostLink = '';
let Icon = [] as any;
let Thumbnail = [] as any;
let IsFree = true;
let PostLaunchDate = Date.now();

async function UploadIcon() {
    if (IconUpload.files) {
        Icon = await appwriteStorage.createFile('646547f5019189c8092b',ID.unique(),IconUpload.files[0]);
    }
}

async function UploadThumbnail() {
    if (ThumbnailUpload.files) {
        Thumbnail = await appwriteStorage.createFile('646547f5019189c8092b',ID.unique(),ThumbnailUpload.files[0]);
    }
}

async function CreatePost() {
    appwriteDatabases.createDocument(
        '646538e35dd17306c589',
        '646538eeeface7a4cd39',
        ID.unique(),
        {
            'title': PostTitle,
            'tagline': PostTagline,
            'description': PostDescription,
            'link': PostLink,
            'isFree': IsFree,
            'launchDate': PostLaunchDate,
            'userID': userID,
            'Icon': IconID,
            'Thumbnail': ThumbnailID
        },
        [
            Permission.read(Role.any()),
            Permission.write(Role.user(userID)),
            Permission.delete(Role.user(userID)),
            Permission.update(Role.user(userID)),
            Permission.delete(Role.team("Owner")),
RangerDev
19 May, 2023, 15:36

Permission.update(Role.team("Owner")), Permission.read(Role.team("Owner")), Permission.delete(Role.team("Admin")), Permission.update(Role.team("Admin")),

TypeScript
    ]
).then((response) => {
    console.log(response);
    UploadIcon();
    UploadThumbnail();
}, (error) => {
    console.log(error);
});

}

</script> ```

RangerDev
19 May, 2023, 15:36

here are my perms

RangerDev
19 May, 2023, 15:36
Binyamin
19 May, 2023, 15:37

I mean in the collection settings?

Binyamin
19 May, 2023, 15:37

Good

Binyamin
19 May, 2023, 15:37

Now you need to add some way for use to create document Either by allow the create permission or inside a cloud-function

RangerDev
19 May, 2023, 15:38

What do you mean?

Binyamin
19 May, 2023, 15:38

Here you've let Any to read Right?

RangerDev
19 May, 2023, 15:38

like this

Binyamin
19 May, 2023, 15:38

Yes

RangerDev
19 May, 2023, 15:38

oh

Binyamin
19 May, 2023, 15:38

Exactly

RangerDev
19 May, 2023, 15:38

let me try now

RangerDev
19 May, 2023, 15:39

do I have it do the same for the buckets?

RangerDev
19 May, 2023, 15:40

why is it giving me the error again? tps://cloud.appwrite.io/v1/databases/646538e35dd17306c589/collections/646538eeeface7a4cd39/documents 401 (anonymous) @ appwrite.209ebd24.js:1 F @ appwrite.209ebd24.js:1 (anonymous) @ appwrite.209ebd24.js:1 (anonymous) @ appwrite.209ebd24.js:1 p @ appwrite.209ebd24.js:1 call @ appwrite.209ebd24.js:1 (anonymous) @ appwrite.209ebd24.js:1 (anonymous) @ appwrite.209ebd24.js:1 p @ appwrite.209ebd24.js:1 createDocument @ appwrite.209ebd24.js:1 I @ create.f48802b9.js:1 create.f48802b9.js:1 AppwriteException: Permissions must be one of: (any, users, user:64664a3a1e3479d80c32, user:64664a3a1e3479d80c32/unverified, users/unverified) at Pe.<anonymous> (https://pitchfork.vercel.app/_astro/appwrite.209ebd24.js:1:19652) at Generator.next (<anonymous>) at c (https://pitchfork.vercel.app/_astro/appwrite.209ebd24.js:1:14540)

Binyamin
19 May, 2023, 15:42

Can you remove the any and try?

Binyamin
19 May, 2023, 15:42

Yes

RangerDev
19 May, 2023, 15:43

From the code or the website

Binyamin
19 May, 2023, 15:45

From the console

But, Sorry In this code

TypeScript
    [
            Permission.read(Role.any()),
            Permission.write(Role.user(userID)),
            Permission.delete(Role.user(userID)),
            Permission.update(Role.user(userID)),
            Permission.delete(Role.team("Owner")),
            Permission.update(Role.team("Owner")),
            Permission.read(Role.team("Owner")),
            Permission.delete(Role.team("Admin")),
            Permission.update(Role.team("Admin")),

        ]

You can't add these line

TypeScript
            Permission.delete(Role.team("Owner")),
            Permission.update(Role.team("Owner")),
            Permission.read(Role.team("Owner")),
            Permission.delete(Role.team("Admin")),
            Permission.update(Role.team("Admin")),
Binyamin
19 May, 2023, 15:46

When you're running against Appwrite from a client side SDK (like Web) you can't set any permission that you don't own. For example in your use case these are the only one you can set from the client side

TypeScript
- any, users, user:64664a3a1e3479d80c32,user:64664a3a1e3479d80c32/unverified, users/unverified

AppwriteException: Permissions must be one of: (any, users, user:64664a3a1e3479d80c32, user:64664a3a1e3479d80c32/unverified, users/unverified)
Binyamin
19 May, 2023, 15:47

If you do want to be able to add and declare as many permissions as you like You can do so, but, for that you'll need to use Appwrite function + Server side SDK

Binyamin
19 May, 2023, 15:47

Hope this makes sense

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