Back

[SOLVED] Unable to create document with $id?

  • 0
  • Databases
  • Flutter
ZachHandley
26 Jul, 2023, 22:40

Hey, trying to create an AppUser document and it's giving me an error saying $id is not allowed for creating new documents, try update instead, do I have to create the document then update it with the wanted $id? I just want the AppUser to have the same ID as the auth user ID

TL;DR
The issue was that the `$id` was included in the `AppUser` object. The solution is to set the document ID as the user ID when creating the document. Here is the corrected code: ```dart Future<AppUser> getUserData(String uid) async { try { final appUserDocument = await db.getDocument( databaseId: AppwriteConstants.databaseId, collectionId: AppwriteConstants.usersCollection, documentId: uid, ); return AppUser.fromMap(appUserDocument.toMap()); } on AppwriteException { final AppUser appUser = AppUser( email:
Binyamin
26 Jul, 2023, 22:41

Set it as the document ID when creating

Binyamin
26 Jul, 2023, 22:41

Can you share the createDocument code

Samuel Kings
26 Jul, 2023, 22:47

Yeah, I think it's because you have id in your user object. It should be passed in the documentId argument of the createDocument method instead

ZachHandley
26 Jul, 2023, 22:48
TypeScript
Future<AppUser> getUserData(String uid) async {
    try {
      final appUserDocument = await db.getDocument(
        databaseId: AppwriteConstants.databaseId,
        collectionId: AppwriteConstants.usersCollection,
        documentId: uid,
      );
      return AppUser.fromMap(appUserDocument.toMap());
    } on AppwriteException {
      final AppUser appUser = AppUser(
        $id: user!.$id,
        email: user!.email,
        name: user?.name,
      );
      final appUserDocument = await db.createDocument(
        databaseId: AppwriteConstants.databaseId,
        collectionId: AppwriteConstants.usersCollection,
        documentId: appUser.$id!,
        data: appUser.toMap(),
      );
      return AppUser.fromMap(appUserDocument.toMap());
    }
  }

sorry should've shared it right away

Binyamin
26 Jul, 2023, 22:49

Change it to this

TypeScript
Future<AppUser> getUserData(String uid) async {
    try {
      final appUserDocument = await db.getDocument(
        databaseId: AppwriteConstants.databaseId,
        collectionId: AppwriteConstants.usersCollection,
        documentId: uid,
      );
      return AppUser.fromMap(appUserDocument.toMap());
    } on AppwriteException {
      final AppUser appUser = AppUser(
        email: user!.email,
        name: user?.name,
      );
      final appUserDocument = await db.createDocument(
        databaseId: AppwriteConstants.databaseId,
        collectionId: AppwriteConstants.usersCollection,
        documentId: user.$id!,
        data: appUser.toMap(),
      );
      return AppUser.fromMap(appUserDocument.toMap());
    }
  }
Binyamin
26 Jul, 2023, 22:50

This way you would set the documentId as the user ID And, it will be at appUserDocument.$id

ZachHandley
26 Jul, 2023, 22:50

OH

ZachHandley
26 Jul, 2023, 22:50

I get it

ZachHandley
26 Jul, 2023, 22:50

$id was in the AppUser, thank you!!

Drake
26 Jul, 2023, 23:14

[SOLVED] Unable to create document with $id?

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