Back

Fetch Failed

  • 0
  • Self Hosted
  • Databases
  • General
ZachHandley
30 Jul, 2024, 01:44

Using 1.5.X (latest, I think current is .7) and my self-hosted server has a lot of "fetch failed"'s happening. I can't tell why, or what's going on, but it's happening sometimes 3-4 times per instance e.g. I am fetching a list of items from the database. Each item pulls a "favorited" status from a favorites collection, but for whatever reason a lot of them are "fetch failed" and granted, it makes a good amount of requests to load each item, it needs the owner, the category, and other things, but. It still (IMO) shouldn't be taking this long with this many "fetch failed"'s

It's also causing my getDocument to fail too

TL;DR
- Developers are facing repeated "fetch failed" errors when trying to get the user's $id. - The issue occurs when attempting to fetch a list of items from the database and retrieving favorited status from a favorites collection. - The problem might be related to the server configuration or network issues causing the failures. - Consider checking the network stability, server logs, and server configuration to troubleshoot. - If the "fetch failed" errors persist, consider optimizing the code to reduce the number of requests being made. - Possible solution: Investigate network stability, server configuration, and optimize code to reduce the number of requests to resolve the "fetch
ZachHandley
30 Jul, 2024, 01:44

for example, this code keeps repeatedly failing to get the user's $id from it

TypeScript
async getOrCreateFavoriteForCurrentUser(): Promise<APIResponse<Favorites>> {
    const user = await tryAwaitWithRetry(async () => await this.account.get());
    return this.getOrCreateFavorite(user.$id);
  }

  /**
   * Retrieves the current favorite of the user or creates a new one if it doesn't exist.
   * @returns The current favorite document.
   */
  async getOrCreateFavorite(userId: string): Promise<APIResponse<Favorites>> {
    try {
      const favorite = await tryAwaitWithRetry(
        async () => await this.getFavorite(userId)
      );
      if (favorite.data) {
        return {
          status: 200,
          data: favorite.data,
          message: "Favorite retrieved successfully",
        };
      } else {
        const createdFavorite = await this.databases.createDocument(
          databaseId,
          APPWRITE_COLL_FAVORITES,
          userId,
          {
            nftIds: [],
            userId: userId,
          }
        );
        return {
          status: 201,
          data: FavoritesSchema.parse(createdFavorite),
          message: "Favorite created successfully",
        };
      }
    } catch (error: any) {
      try {
        const newFavorite = {
          nftIds: [], // Initialize with an empty array or any default values
          userId: userId,
        };
        const createdFavorite = await tryAwaitWithRetry(
          async () =>
            await this.databases.createDocument(
              databaseId,
              APPWRITE_COLL_FAVORITES,
              userId,
              newFavorite,
              getDefaultPermissions(userId)
            )
        );
        return {
          status: 201,
          data: FavoritesSchema.parse(createdFavorite),
          message: "Favorite created successfully",
        };
      } catch (createError) {
        if (import.meta.env.DEV) {
          console.error(createError);
        }
        return {
          status: 500,
          message: "Error creating favorite",
        };
      }
    }
  }
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