Back

[SOLVED] Not able to interact with database

  • 0
  • Databases
  • Web
  • Cloud
Aman
11 Jan, 2025, 17:17

Can anyone help me? I have used Appwrite multiple times before, but this time, I created a new project, and it is not interacting with the database. It's a fresh project, and I even tried recreating the project, but it still didn’t work. You can see the response I’m getting from the image below.

TL;DR
Developers were facing issues with database interaction, specifically in not being able to create documents. The solution involved ensuring the project ID was passed correctly in the GET request. The issue was resolved by correcting the project ID error in the code snippet provided.
Kenny
11 Jan, 2025, 17:22

Are you sure your project id is defined?

Aman
11 Jan, 2025, 17:27

yes

Kenny
11 Jan, 2025, 17:30

can you verify that for me? Console log it?

Aman
11 Jan, 2025, 17:30

see

Kenny
11 Jan, 2025, 17:31

so, the get document by id function is saying the document cannot be found? Is that the only issue?

Aman
11 Jan, 2025, 17:34

Documents are not being created. I tried adding data manually for the user ID, but it still shows an error when trying to fetch. manualdata entry is only working

Kenny
11 Jan, 2025, 17:35

Could you show the error from your post request for making a document?

Aman
11 Jan, 2025, 17:39

actually this time post req working , only get req show error

Aman
11 Jan, 2025, 17:40

idk how previously giving error

Kenny
11 Jan, 2025, 17:40

I would think that would be the case in your sync function?

Kenny
11 Jan, 2025, 17:40

If the user document does exist it will throw and error and create that document?

Aman
11 Jan, 2025, 17:42

see this data is passing

export const syncUserWithAppwrite = async (user) => { const { id, username, emailAddresses } = user; console.log("Syncing user with Appwrite:", id, username, emailAddresses[0].emailAddress); try { try { // Try to fetch the document by user id const existingUser = await databases.getDocument( conf.databaseID, conf.UsersCollectionID, id ); // If user exists, skip adding if (existingUser) { console.log('User already exists in Appwrite'); return; // or handle the case where user already exists } } catch (error) { // Handle the case where the document does not exist if (error.message.includes('Document not found')) { // Add user to Appwrite if not exists await databases.createDocument(conf.databaseID, conf.UsersCollectionID, { userId: id, username: username, email: emailAddresses[0].emailAddress, }); } else { console.error('Error checking existing user:', error); } } } catch (error) { console.error("Error syncing user:", error); } };

for this function

but i tried to add the data manually to database then also not fetching

Kenny
11 Jan, 2025, 17:43

are you sure the document you created manually has the id you're looking for?

Aman
11 Jan, 2025, 17:44

yep see

Kenny
11 Jan, 2025, 17:45

you need to use the document id

Kenny
11 Jan, 2025, 17:45

which is not user_guid

Aman
11 Jan, 2025, 17:47

Oh shit, how i made a silly mistake. Thank you, sir, for helping!

Kenny
11 Jan, 2025, 17:48

haha no problem, sometimes a second set of eyes is all you need

Aman
11 Jan, 2025, 17:48

i thought i was passing same data for the Id and userId

Kenny
11 Jan, 2025, 17:48

lmk if that fixes it for you

Kenny
11 Jan, 2025, 17:49

as for that other image about the project id, I think that's just because you didn't pass the project id header in the get request.

Aman
11 Jan, 2025, 17:52

I also thought the project ID wasn’t being passed, which is why it was giving an error. I didn’t check it carefully. There’s so much to learn thanks

Kenny
11 Jan, 2025, 17:53

[SOLVED] Not able to interact with database

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