Back

Need help getting currentUser by AccountID

  • 0
  • React Native
  • Apple
Jeratomb
27 Sep, 2024, 09:06

Hey im having some troubles with getting the currentUser. I managed to get the Account with account.get but when i want to do this to get the currentUser the object in console is null

export const getAccount = async () => { try { const currentAccount = await account.get(); console.log(currentAccount); return currentAccount; } catch (error) { throw new Error(error); } }

export const getCurrentUser = async () => { try { const currentAccount = await getAccount(); if (!currentAccount) throw new Error('Account not found');

TypeScript
    const currentUser = await databases.listDocuments(
        appwriteConfig.databaseId,
        appwriteConfig.userCollectionId,
        [Query.equal('id', currentAccount.$id)] // Ensure 'accountId' exists in the schema
    );
    console.log(currentUser)
    if (!currentUser) throw new Error('User not found');

    return currentUser.documents[0];
} catch (error) {
    console.log(error);
    return null;
}

} can someone maybe help?

TL;DR
Developers are struggling to get the currentUser by AccountID. They are facing issues with collections, documents, and ID attributes. Solution: Update the query in getCurrentUser function to use `database.getDocument()` instead of `listDocuments()` and ensure the 'id' attribute exists in the schema for the user collection.
Jeratomb
27 Sep, 2024, 09:08

message: "Invalid query: Attribute not found in schema: id" this is the error message in devtools

i tried with accountid aswell but doesnt work

D5
27 Sep, 2024, 09:10

Do you have added a collection with an attribute called id? If you want to get a single document by it's ID, use database.getDocument() instead of listDocuments()

Jeratomb
27 Sep, 2024, 09:36

ok tried it with get.Document() but now i get invalid documentID error

D5
27 Sep, 2024, 09:39

Do you have a collection and a document with that id?

D5
27 Sep, 2024, 09:39

Is there any document being shown in the console?

Jeratomb
27 Sep, 2024, 09:41

i get the whole account object in console with an id attribute

Jeratomb
27 Sep, 2024, 09:42

i dont really knwo what u mean by collection and document. I just read on the docs that this is how to get the currentUser for the session but thats not working xD

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