Back

Database Not Found Error in Next.js Application with Appwrite Backend

  • 0
  • Databases
CriptoPoeta
30 Jul, 2024, 02:59

I'm encountering a persistent "Database not found" error in my Next.js application that uses Appwrite as the backend. Despite following the documentation and checking my configuration, I'm unable to resolve this issue. Below are the details: Environment:

Next.js (Version: [Your Next.js version]) Appwrite SDK (Version: [Your Appwrite SDK version]) Node.js (Version: [Your Node.js version])

Error Message: CopyAppwriteException: Database not found at _Client.call (webpack-internal:///(rsc)/./node_modules/node-appwrite/dist/client.mjs:283:13) ... code: 404, type: 'database_not_found', response: { message: 'Database not found', code: 404, type: 'database_not_found', version: '1.5.8' } Context: I'm trying to create a user document in my Appwrite database when a new user is created through a third-party authentication service (Dynamic). The error occurs when trying to execute the createDocument method of the Appwrite SDK.

Code Structure:

I have an appwrite.ts file that sets up the Appwrite client:

import { Client, Account, Databases, Users } from "node-appwrite";

export async function createAdminClient() { const client = new Client() .setEndpoint(process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT!) .setProject(process.env.NEXT_PUBLIC_APPWRITE_PROJECT!) .setKey(process.env.NEXT_APPWRITE_KEY!);

return { database: new Databases(client), }; }

TL;DR
Issue: When trying to create a user document in an Appwrite database using a Next.js application, a "Database not found" error is persistently occurring despite correct configurations. Solution: - Ensure that the DATABASE_ID matches an existing database in the Appwrite project and verify other environment variables. - Confirm that the API key has the necessary permissions to access and modify the database. - Check that permissions in each collection are set to All for CRUD operations. - Try hardcoding database and collection IDs instead of using environment variables. Environment: - Next.js - Appwrite SDK - Node.js Error: - CopyAppwriteException: Database
CriptoPoeta
30 Jul, 2024, 02:59

In my dynamic.user.actions.ts file, I have a createUser function:

const { APPWRITE_DATABASE_ID: DATABASE_ID, APPWRITE_USERS_COLLECTION_ID: USERS_COLLECTION_ID, APPWRITE_WALLETS_COLLECTION_ID: WALLETS_COLLECTION_ID, APPWRITE_VERIFIED_CREDENTIALS_COLLECTION_ID: VERIFIED_CREDENTIALS_COLLECTION_ID, } = process.env;

export const createUser = async (userData: CreateUserProps) => { try { const { database } = await createAdminClient(); console.log("New user data", userData);

TypeScript
const newUser = await database.createDocument(
  DATABASE_ID,
  USERS_COLLECTION_ID,
  userData.id,
  {
    userId: userData.id,
    email: userData.email,
    // ... other user fields ...
  }
);

// ... code to create related documents ...

return parseStringify(newUser);

} catch (error) { console.error("An error occurred while creating the user:", error); throw error; } };

Steps to Reproduce:

A new user signs up through the Dynamic authentication service. The webhook is triggered, calling my createUser function. The function attempts to create a new document in the Appwrite database. The "Database not found" error is thrown.

What I've Tried:

Triple-checked that the DATABASE_ID matches an existing database in my Appwrite project, as well with other env variables. Verified that the API key has the necessary permissions to access and modify the database. Verified permissions in each collection are set to All for all CRUD options. Tried hardcoding the database and collection IDs instead of using environment variables. Logged the database ID and other relevant information before the createDocument call and all variables are correctly passed.

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