Back

Getting form file input from server side (Svelte)

  • 0
  • Self Hosted
  • Web
  • Storage
Binyamin
16 Jul, 2023, 22:51

I'm not sure about SvelteKit But I think this line

TypeScript
 post: {
  bucketId: createBucket
 }

Will be sent sync, meaning you won't get the createBucket value. You must add some await in that post

TL;DR
The user is asking for support regarding form file input from the server side in Svelte. They have created multiple buckets for each set of images but are facing issues with retrieving the bucket ID on the client side. Another user suggests waiting for the `createProject` function to get the bucket ID. The user is advised to add the `await` keyword in the `post` section to get the `createBucket` value.
BloodThermic
16 Jul, 2023, 22:51
TypeScript
    return {
        post: {
            bucketId: await createBucket
        }
    };
}```
I can do this
Binyamin
16 Jul, 2023, 22:53

But you'll to wait to the createProject function

Or something similar to this

TypeScript
async function getBucket() {
  const bucketId = await actions.createProject(/* Pass your data*/);
  return bucketId;
}

export async function load() {
    return {
        post: {
            bucketId: await getBucket()
        }
    };
}
BloodThermic
16 Jul, 2023, 22:55
TypeScript

import sdk, { ID, Permission, Role } from 'node-appwrite';
import { PUBLIC_API_ENDPOINT, PUBLIC_PROJECT_ID } from '$env/static/public';
import { PRIVATE_API_KEY } from '$env/static/private';

const client = new sdk.Client();
// const account = new sdk.Account(client);
// const databases = new sdk.Databases(client);
const storage = new sdk.Storage(client);

client
    .setEndpoint(PUBLIC_API_ENDPOINT) // Your API Endpoint
    .setProject(PUBLIC_PROJECT_ID) // Your project ID
    .setKey(PRIVATE_API_KEY); // Your secret API key

let createBucket;

export const actions = {
    createProject: async ({ request }) => {
        const formData = await request.formData();
        const Client = formData.get('Client');

        createBucket = await storage.createBucket(ID.unique(), Client, [
            Permission.create(Role.user('64b1c7848008d470d744')),
            Permission.update(Role.user('64b1c7848008d470d744')),
            Permission.read(Role.user('64b1c7848008d470d744'))
        ]);

        // bucketId = createBucket.$id

        console.log(createBucket.$id);

        // console.log(Client);
    },
    imgUpload: async ({ request }) => {}
};

export async function load() {
    return {
        post: {
            bucketId: createBucket
        }
    };
}

This works just fine. So here is the output of the id on the client-side

BloodThermic
16 Jul, 2023, 22:56

This one is the id of the bucket it created before this last one. 64b4752051094e8fd08f Outside timeout

This one is correct 64b4753caa58d8fe56de Outside timeout

Binyamin
16 Jul, 2023, 22:58

So you first send a form action to createPost and only then you're checking for data.post.bucketId? From what I see the second will happened first make you get the bucket ID each time.

BloodThermic
16 Jul, 2023, 22:59
TypeScript
        action="?/createProject"
        method="post"
        on:submit|preventDefault={onSubmit}
        class="flex flex-col justify-center mt-5"
        use:enhance
    >```
BloodThermic
16 Jul, 2023, 23:00

use:enhance allows me to send form data to both client and server side using post method for server side and on:submit for client side function

Binyamin
16 Jul, 2023, 23:02

I see, Probably it best to wait for some one more expert in using <:svelte:1084088345824591912>

What I think it has to do with the fact that bucketId is sent back before the form has ben submitted

BloodThermic
16 Jul, 2023, 23:03

The form is submitted when the onSubmit funtion runs and when the from actions is ran

BloodThermic
16 Jul, 2023, 23:05

Thank you for your time

BloodThermic
16 Jul, 2023, 23:07

Getting form file input from server side (Svelte)

Drake
17 Jul, 2023, 01:28

Why are you creating buckets at runtime like this?

BloodThermic
17 Jul, 2023, 01:40

I was going to create a bucket for each set of image to keep database organized

Drake
17 Jul, 2023, 01:42

What do you mean by set of image?

BloodThermic
17 Jul, 2023, 01:43

Set of images sorry.

BloodThermic
17 Jul, 2023, 01:43

Like one project Might have 4 img and I want then to all be together

BloodThermic
17 Jul, 2023, 01:50

@Steven

Drake
17 Jul, 2023, 01:52

For each user? I still don't really understand

BloodThermic
17 Jul, 2023, 02:08

Each of these is a project. This is for a portfilio page, I want to have a set of img for each project that is in their own buckets

Drake
17 Jul, 2023, 02:23

I typically advise against creating collections or buckets at runtime like this

BloodThermic
17 Jul, 2023, 02:24

Interresting why is that?

BloodThermic
17 Jul, 2023, 02:24

just like complications

Drake
17 Jul, 2023, 02:26

A simple flat design is easier to maintain. A collection or bucket should be a type of data. If you have the same type of data in multiple collections or buckets, you should really have a good reason for it

BloodThermic
17 Jul, 2023, 02:27

Ok thank you I will just stay with one storage bucket then

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