Back

getting a error at fetching data from appwrite services

  • 0
  • Web
Yukti
29 Jan, 2024, 08:13

Error : DataService :: getExpenses :: error: AppwriteException: Server Error at Client.<anonymous> (http://localhost:5173/node_modules/.vite/deps/appwrite.js?v=60898c8d:850:17) at Generator.next (<anonymous>) at fulfilled (http://localhost:5173/node_modules/.vite/deps/appwrite.js?v=60898c8d:488:24)

please help i need to retirve specific user data by using their userId code :

TypeScript
async saveExpense({ expenseName, expense, expenseDate }) {
    try {
        const userId = localStorage.getItem('userId');

        if (!userId) {
            throw new Error("User not authenticated");
        }

        const data = {
            userId,
            expenseName,
            expense,
            expenseDate
        };

        return await this.databases.createDocument(
            conf.appwriteDatabaseId,
            conf.appwriteCollectionId,
            ID.unique(),
            data
        );
    } catch (error) {
        console.log("DataService :: saveExpense :: error: ", error);
        throw error;
    }
}

async getExpenses() {
    try {
        const userId = localStorage.getItem('userId');

        if (!userId) {
            throw new Error("User not authenticated");
        }

        const response = await this.databases.listDocuments(
            conf.appwriteDatabaseId,
            conf.appwriteCollectionId,
            ['*'],
            100,
            0,
            `userId=${userId}`
        );

        return response.documents;
    } catch (error) {
        console.log("DataService :: getExpenses :: error: ", error);
        throw error;
    }
}

}

const dataService = new DataService(); export default dataService;

TL;DR
Developers are encountering an error when fetching data from Appwrite services. They are trying to retrieve data entered by a specific user using their `userId`. The error message they are receiving is `AppwriteException: Server Error`. They need help resolving this issue. Solution: - Make sure the database ID is correct. - Check if there is an attribute with the user ID in the database. - Verify that the `userId` is being set correctly in the `saveExpense` and `getExpenses` functions. - In the `getExpenses` function, modify the code to: ```javascript const response = await this.d
D5
29 Jan, 2024, 08:16

Are you sure database ID is correct?

D5
29 Jan, 2024, 08:17

Also what's userID=${userId} for?

Yukti
29 Jan, 2024, 08:17

yes i am

Yukti
29 Jan, 2024, 08:18

userid is to retrieve data entered by specific user

D5
29 Jan, 2024, 08:23

Do you have an attribute with the user ID?

D5
29 Jan, 2024, 08:26

If so, it should be like this:

TypeScript
const response = await this.databases.listDocuments(
                conf.appwriteDatabaseId,
                conf.appwriteCollectionId,
                [
                  Query.limit(100),
                  Query.equal("userID",[userId]),
                ],
            );
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