Back

AppwriteException: Invalid document structure: Missing required attribute "userId"

  • 0
  • Databases
  • Accounts
  • Web
  • Users
Himanshu
13 Feb, 2024, 08:28

I am getting this error at the line console.log("Error while creating a user in 'user' database, saveUserToDB: ", error); console.log(name, email, username, userId); prints two times, first time, it is printing the value of userId as expected but at the second time that prints undefined. But when I checked my collection, the records are getting created then why this error?

auth.js import { ID } from "appwrite"; import { account, avatars } from "./config"; import { saveUserToDB } from "./db";

export const createAccount = async ({ email, password, name, username }) => { try { const userAccount = await account.create(ID.unique(), email, password, name);

TypeScript
    if (userAccount) {
        console.log(userAccount.$id);
        const avatarUrl = avatars.getInitials(name);

        await saveUserToDB({ name: name, email: email, username: username, userId: userAccount.$id, imageUrl: avatarUrl });

        return login({ email, password });
    } else {
        return userAccount;
    }
} catch (error) {
    throw error;
}

}

export const login = async ({ email, password }) => { try { const session = await account.createEmailSession(email, password); if (session) { return session } else { throw new Error("Failed to create a session"); } } catch (error) { throw error; } }

export const getCurrentUser = async () => { try { const currentUser = await account.getprefs(); return currentUser; } catch (error) { console.log("Appwrite error on getCurrentUser: ", error); } return null; }

export const logout = async () => { try { return await account.deleteSessions(); } catch (error) { console.log("Appwrite Service error on userLogout: ", error); } }

TL;DR
The developer is encountering the error "Invalid document structure: Missing required attribute 'userId'" while trying to create a user in the database. The userId attribute is being printed correctly the first time, but undefined the second time. However, when checking the collection, the records are successfully created. Solution: The issue might be with the way the saveUserToDB function is being called. Check if all the required parameters are being passed correctly. Additionally, ensure that the ID is unique for each user being created.
Himanshu
13 Feb, 2024, 08:28

Another file db.js import { client, databases, storage, appwriteConfig } from "./config"; import { ID } from "appwrite";

export const saveUserToDB = async ({ name, email, username, userId, imageUrl }) => { try { console.log(name, email, username, userId); return await databases.createDocument( appwriteConfig.databaseId, appwriteConfig.userCollectionId, ID.unique(), { name, email, username, userId, imageUrl } ); } catch (error) { console.log("Error while creating a user in 'user' database, saveUserToDB: ", error); } }

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