Back

Getting error when trying to execute a function on a DB

  • 0
  • Databases
  • Functions
  • Web
Shlok
30 Oct, 2024, 18:12

Hi all, I am trying to create a function which connects to my db but I am getting a database not found error. I have put the exact db & collection IDs but still there is error. I have tried sending a simple text response & that worked, means path and everything is set correctly. I have also given correct permissions in db and collections. I have also selected All in the Scopes of my Function. But when I am trying to do a listDocuments call to my db, it is saying db not found.

I have used this tutorial for this https://www.youtube.com/watch?v=PmEIu-qrcac&list=PL-nc7zI7zjsbGzYBiG_V3tEFpmlzzbJgC&index=4

Here is my code. I tried replacing the IDs of the project, db and collections with the exact id, but still it is an issue. The star mark is where I put my IDs. import { Client, Databases } from "node-appwrite";

export default async ({ req, res, log, error }) => {

TypeScript
const client = new Client()
client
    .setEndpoint('https://cloud.appwrite.io/v1')
    .setProject('project id here ***')

const db = new Databases(client)
if (req.method == 'GET') {
    const response = await db.listDocuments(
        'db id here ***',
        'collcetions id here ***'
    )
    return res.json(response.documents)
}

return res.send("Shlok, tesing if user delete worked")

}

Below is my error -

Error: Database not found at Client.call (/usr/local/server/src/function/node_modules/node-appwrite/lib/client.js:168:31) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async Databases.listDocuments (/usr/local/server/src/function/node_modules/node-appwrite/lib/services/databases.js:905:16) at async Module.default (file:///usr/local/server/src/function/functions/main.js:12:26) at async execute (/usr/local/server/src/server.js:208:16) at async action (/usr/local/server/src/server.js:225:7) at async /usr/local/server/src/server.js:14:5

TL;DR
Developers are facing an error "Database not found" when trying to execute a function on a database even though the project, database, and collection IDs are correct. Permissions on the collection are mentioned to be correct, however, the error persists. Check and validate the project ID, database ID, and collection ID for correctness. If the issue remains, it is suggested to share the code snippet and the URL of the collection settings page on the Console for further assistance. Wrapping code in backticks is advised for better formatting.
Kenny
30 Oct, 2024, 18:15

Is your project ID correct?

Shlok
30 Oct, 2024, 18:16

yes

Steven
30 Oct, 2024, 18:22

FYI, it's best to wrap code in backticks to format a bit nicer. You can use 1 backtick for inline code (https://www.markdownguide.org/basic-syntax/#code) and 3 backticks for multiline code (https://www.markdownguide.org/extended-syntax/#syntax-highlighting).

Shlok
30 Oct, 2024, 18:27

Thanks Steven, I will keep that in mind next time.

Steven
30 Oct, 2024, 18:32

what are the permissions on your collection?

Shlok
30 Oct, 2024, 18:37

all ticked

Steven
30 Oct, 2024, 18:39

double check your project id, database id, and collection id are correct. If you're still having trouble, please share exactly what you're using in your code and the URL of this collection settings page on the Console

Shlok
30 Oct, 2024, 18:55

This is the structure of my project - attached in image. There is an .env file where I create all the variables for Appwrite IDs. Example: VITE_PROJECT_ID=671f17fb0030e3b17ebc

Now I have configured all three environment variables - Project id, database id & collection id in console -> Function Environment variables.

Here is the collection settings page on the Console https://cloud.appwrite.io/console/project-671f17fb0030e3b17ebc/databases/database-671f2f4f00014e62eae3/collection-671f3276001caa90944d/settings

Here are the contents of my main.js file

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


const PROJECT_ID = process.env.VITE_PROJECT_ID
const DATABASE_ID = process.env.VITE_DATABASE_ID
const COLLECTION_ID_ALLUSERS = process.env.VITE_COLLECTION_ID_ALLUSERS

export default async ({ req, res, log, error }) => {

    const client = new Client()
    client
        .setEndpoint('https://cloud.appwrite.io/v1')
        .setProject(PROJECT_ID)

    const db = new Databases(client)
    if (req.method == 'GET') {
        const response = await db.listDocuments(
            DATABASE_ID,
            COLLECTION_ID_ALLUSERS
        )
        return res.json(response.documents)
    }

    return res.send("Shlok, tesing if user delete worked")
}

Latest execution result

TypeScript
Error: Database not found
    at Client.call (/usr/local/server/src/function/node_modules/node-appwrite/lib/client.js:168:31)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Databases.listDocuments (/usr/local/server/src/function/node_modules/node-appwrite/lib/services/databases.js:905:16)
    at async Module.default (file:///usr/local/server/src/function/functions/main.js:17:26)
    at async execute (/usr/local/server/src/server.js:208:16)
    at async action (/usr/local/server/src/server.js:225:7)
    at async /usr/local/server/src/server.js:14:5
Steven
30 Oct, 2024, 18:59
TypeScript
const PROJECT_ID = process.env.VITE_PROJECT_ID
const DATABASE_ID = process.env.VITE_DATABASE_ID
const COLLECTION_ID_ALLUSERS = process.env.VITE_COLLECTION_ID_ALLUSERS

Would you please log these variables to verify the correct values are being used?

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