Back

Cors On Updating Document

  • 0
  • Databases
  • Web
  • Cloud
aliyorulmazdev
13 Aug, 2024, 09:20

Hello, create and delete functions works well with custom domain appwrite database with the code above. but updating doesnt work.

TypeScript
import client from "@/app/appwrite";
import { Databases, ID, Query } from "appwrite";

class AddressService {
  static instance;
  static databases = new Databases(client);
  static databaseId = "";
  static collectionId = "";

  static getInstance() {
    if (!AddressService.instance) {
      AddressService.instance = new AddressService();
    }
    return AddressService.instance;
  }
  createAddress = async (userId, addressName, fullAddress) => {
    try {
      return await AddressService.databases.createDocument(
        AddressService.databaseId,
        AddressService.collectionId,
        ID.unique(),
        {
          user_id: userId,
          address_name: addressName,
          full_address: fullAddress,
        }
      );
    } catch (error) {
      throw error;
    }
  };
  updateAddress = async (documentId, addressName, fullAddress) => {
    try {
      const result = await AddressService.databases.updateDocument(
        AddressService.databaseId,
        AddressService.collectionId,
        documentId, 
        {
          address_name: addressName,
          full_address: fullAddress,
        }
      );
      console.log("Document updated successfully:", result);
      return result; 
    } catch (error) {
      console.error("Error updating address:", error);
      throw error;
    }
  };
  deleteAddress = async (documentId) => {
    try {
      await AddressService.databases.deleteDocument(
        AddressService.databaseId,
        AddressService.collectionId,
        documentId
      );
      console.log("Document deleted successfully");
    } catch (error) {
      console.error("Error deleting document:", error);
      throw error;
    }
  };
export default AddressService;
TL;DR
The developer found a temporary solution for updating a document by creating an API route instead of calling the service directly. They shared code snippets demonstrating the creation, update, and delete functions, with the issue specifically with updating not working as expected. The solution involves reviewing and potentially revising the updateAddress function in the AddressService class.
aliyorulmazdev
13 Aug, 2024, 09:22

Temporary solved it with ;

TypeScript
  updateAddress = async (documentId, addressName, fullAddress) => {
    try {
      // Mevcut belgeyi al
      const existingDocument = await AddressService.databases.getDocument(
        AddressService.databaseId,
        AddressService.collectionId,
        documentId
      );

      // Mevcut belgeyi sil
      await AddressService.databases.deleteDocument(
        AddressService.databaseId,
        AddressService.collectionId,
        documentId
      );

      // Yeni belge oluştur
      const result = await AddressService.databases.createDocument(
        AddressService.databaseId,
        AddressService.collectionId,
        documentId, // Aynı ID ile yeni belge oluşturuluyor
        {
          user_id: existingDocument.user_id, // Mevcut belgeden alınan veriler
          address_name: addressName,
          full_address: fullAddress,
        }
      );

      console.log("Document updated successfully:", result);
      return result; // Güncellenmiş belgeyi döndür
    } catch (error) {
      console.error("Error updating address:", error);
      throw error; // Hata oluşursa fırlat
    }
  };
``` but really need it to be fixed.
aliyorulmazdev
13 Aug, 2024, 09:32

Solved it with creating API route instead of calling service directly on client client component.

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