Skip to content
Back

account.updatePushTarget gives AppwriteException document_already_exists

  • 0
  • Flutter
  • Cloud
Seroga88i
16 Jan, 2025, 07:36

Hello guys,

I face again a problem with the account.updatePushTarget API. Now I am calling the updatePushTarget and I get the AppwriteException document_already_exists, not sure what the problem is and what the exception wants to tell me in this context. I am using the appwrite beta cloud and I make the call from my flutter mobile application my sdk version is appwrite: 13.0.0

This is my flutter code:

TypeScript
Future<void> setUserTarget(String? fcmToken, User user) async {
    if (fcmToken == null || fcmToken.isEmpty) {
      return;
    }
    try {
      final existingPushTarget = user.targets
          .where((target) => target.providerType == 'push')
          .firstOrNull;
      if (existingPushTarget == null) {
        // Create new target
        await _account.createPushTarget(
          targetId: ID.unique(),
          identifier: fcmToken,
          providerId: PROVIDER_ID,
        );
      } else if (existingPushTarget.identifier != fcmToken) {
        // Update existing target if token is different
        await _account.updatePushTarget(
          targetId: existingPushTarget.$id,
          identifier: fcmToken,
        );
      }
    } on AppwriteException catch (e, stackTrace) {
      if (kDebugMode) {
        print('Error setting user target: $e');
      }
      rethrow; // Rethrow to handle in calling code
    }
  }

And the response is attached in the screenshot.

Please let me know what the problem could be

Kind regards

TL;DR
Issue: Developer receives AppwriteException `document_already_exists` when using `account.updatePushTarget` API in Flutter mobile application. Solution: The exception `document_already_exists` indicates that the document (push target) being updated already exists. To resolve this, ensure that for an existing push target, the `updatePushTarget` call is updating an existing record instead of creating a new one. Check the unique identifier being used and verify if the document needs to be updated or created based on the scenario.
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