Skip to content
Back

A target with the same ID already exists

  • 0
  • Flutter
  • Messaging
  • Cloud
Seroga88i
15 Oct, 2024, 19:28

Hello guys, I am implementing a flutter application which communicates with the cloud appwrite system. On the login step I try to add a push target if this target is missing. The code for this looks like:

TypeScript
Future<void> setUserTarget(String? fcmToken, User user) async {
    if (fcmToken != null) {
      if (user.targets.isEmpty ||
          !user.targets.any((targe) => targe.providerType == 'push')) {
        await _account.createPushTarget(
            targetId: user.$id,
            identifier: fcmToken,
            providerId: PROVIDER_ID);
      } else {
        await _account.updatePushTarget(
            targetId: user.$id, identifier: fcmToken);
      }
    }
  }

When I debug through the code I get this state of the user is targest list is empty (see screenshot), but after caling the _account.createPushTarget() function I get the error message: A target with the same ID already exists (see the second screenshot) I also checked the appwrite console and there is not target for the user.

I am not sure what the problem is. Can anybody help?

Kind regards

TL;DR
- Developer is facing issues with targets in a Flutter application connecting to Cloud Appwrite system. - Changed code to use `ID.unique()` instead of user ID for target ID, but problem persists. - Discussion on user objects and targets. - Solution: The issue might be due to duplicates or conflicts with targets. Investigate further and potentially use different unique identifiers for targets.
Kenny
15 Oct, 2024, 19:35

How are you getting the user object?

Seroga88i
15 Oct, 2024, 19:36

With this line of code: final user = await _account.get();

Kenny
15 Oct, 2024, 19:38

If you look in console under that user do you see the target for that already?

Seroga88i
15 Oct, 2024, 19:39

No, there is no target for that user

Seroga88i
15 Oct, 2024, 19:40
Kenny
15 Oct, 2024, 19:41

is there any reason you're using the users id as the target ID and not ID.Unique()?

Seroga88i
15 Oct, 2024, 19:43

Hmm, this is a good point. I was thinking I saw this in the documentation. But, I should use ID.unique()

Seroga88i
15 Oct, 2024, 19:43

I will try that

Kenny
15 Oct, 2024, 19:44

Not that it should be conflicting now since there isn't one, but I'm not sure what it validates for that section off the top of my head.

Seroga88i
15 Oct, 2024, 19:47

I looked through the documentation and found this: https://appwrite.io/docs/references/cloud/server-nodejs/users#updateTarget But I mixed it up. I will try with ID.unique and will let you if the problem is solved

Seroga88i
15 Oct, 2024, 19:53

I changed my code, now using ID.unique() But I am still facing the same problem

Seroga88i
15 Oct, 2024, 19:58

Ok, now something is completely off. I want to try to login with a different user to check if it works or not. Now I get the problem with session_already_exists but nothing is visible in the console

Seroga88i
15 Oct, 2024, 19:59

I am not sure what is happening 😰

Kenny
15 Oct, 2024, 19:59

:( Did you delete the session from your last user?

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