Back

Sveltekit + simple Appwrite database

  • 0
  • Databases
  • Web
JackBiscuits
25 Mar, 2024, 19:30

Hi!,

I would like to make a simple Appwrite database that updates emails and cities each time two text fields in my sveltekit website is filled. Very simple. However, it doesnt seem to function. here is the main code:

TypeScript
<script lang="ts">

export async function getIdeas() {
    return await databases.listDocuments(
        DATABASE_ID,
        COLLECTION_ID,
        // Use a query to show the latest ideas first
        [Query.orderDesc('$createdAt')]
    );
}

export async function addIdea(Email, CIty) {
    await databases.createDocument(DATABASE_ID, COLLECTION_ID, ID.unique(), {
        Email,
        CIty
  });
}

export async function deleteIdea(id) {
    await databases.deleteDocument(DATABASE_ID, COLLECTION_ID, id);
}

async function handleSubmit() {
    try {
      if (!Email || !CIty) throw new Error("Please fill out both fields.");
      
      // Add the idea with email and city to the database
      await addIdea(Email, CIty);
      
      // Clear input fields after successful submission
      Email = '';
      CIty = '';

      alert("Thank you! You've been added to our notification list.");
    } catch (error) {
      console.log(error);
      alert("Something went wrong while submitting your request. Please try again later.");
    }
  }


</script>

<main class="container px-6 py-8 mx-auto max-w-screen-md">
  <h2 class="mb-5 text-xl font-bold leading-tight tracking-tighter md:text-2xl"> subscribe</h2>
  <div class="grid grid-cols-2 gap-4 w-full">
    <FlowbiteInput bind:value={Email} type="email" placeholder="Your Email Address" />
    <FlowbiteInput bind:value={CIty} type="text" placeholder="Your City" />
  </div>
  <button on:click={handleSubmit} >Subscribe</button>
</main>

i simply follow the guide on the website, but it seems to not communicate with my database_id and collection id

TL;DR
Developers are trying to create a simple Appwrite database with SvelteKit but are facing issues with communication with the database. They are encountering errors and have questions about error handling and initializing the Appwrite client. The code provided showcases functions for adding, deleting, and getting ideas from the database. To address the issue, developers should ensure proper initialization of the Appwrite client and handle errors effectively using try-catch blocks.
Kenny
25 Mar, 2024, 19:34

Where are you initializing the appwrite client?

JackBiscuits
25 Mar, 2024, 19:44

I am initializing it at src/lib/appwrite.js:

TypeScript

const client = new Client();
client
  .setEndpoint("https://cloud.appwrite.io/v1")
  .setProject("project id"); // Replace with your project ID

export const account = new Account(client);
export const databases = new Databases(client); ```
Kenny
25 Mar, 2024, 19:45

Are you having any errors thrown?

JackBiscuits
25 Mar, 2024, 19:46

before running it does say "Parameter 'Email' implicitly has an 'any' type." in vs code.

I also get the self-made error:

""Something went wrong while submitting your request. Please try again later.""

JackBiscuits
25 Mar, 2024, 19:56

How can i do error handling? how can see wht goes wrong in appwrite?

Kenny
25 Mar, 2024, 19:57

You can wrap your appwrite code in a try catch block.

Kenny
25 Mar, 2024, 19:59

Could you in addIdea do something like

TypeScript
export async function addIdea(Email, CIty) {
    console.log(DATABASE_ID);
    console.log(COLLECTION_ID);

    try {
      await databases.createDocument(DATABASE_ID, COLLECTION_ID, ID.unique(), {
           Email,
           CIty
         }
      );
    } catch (error: any) {
      console.log(error);
    }
}
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