Back

Using Functions to get list of Documents

  • 0
  • Databases
  • Functions
Zionnite
13 Nov, 2023, 07:47

I'm trying to get the list of documents with the Appwrite function, unfortunately, I can't get any value.

const sdk = require("node-appwrite"); module.exports = async function (req, res) { const client = new sdk.Client(); // You can remove services you don't use const database = new sdk.Databases(client); if ( !req.variables["APPWRITE_FUNCTION_ENDPOINT"] || !req.variables["APPWRITE_FUNCTION_API_KEY"] ) { console.warn( "Environment variables are not set. Function cannot use Appwrite SDK." ); } else { client .setEndpoint(req.variables["APPWRITE_FUNCTION_ENDPOINT"]) .setProject(req.variables["APPWRITE_FUNCTION_PROJECT_ID"]) .setKey(req.variables["APPWRITE_FUNCTION_API_KEY"]) .setSelfSigned(true); } const DbCollection = "64bb1ee4200b221e0675"; const usersCollection = "64bb1f396ed41ad9c1d8"; const prayerPartnerCollection = "6545e03a11aa6b5ba77a"; const prayerPartnerHistoryCollection = "6545e4ba5bcad02198c0"; const notificationCollection = "654cd8010d7f67c0d238"; const promise = database.listDocuments(DbCollection, usersCollection); promise.then( function (response) { res.json({ myResponse: response, }); }, (error) { console.log(error); res.json({ myError: error, endPoint: req.variables["APPWRITE_FUNCTION_ENDPOINT"], projectId: req.variables["APPWRITE_FUNCTION_PROJECT_ID"], functionApi: req.variables["APPWRITE_FUNCTION_API_KEY"], }); } ); };

over the past 7hrs, I have been reading appwrite documentation on how to make this code work, but nothing I do seems to work, i have to try PHP runtime yet, nothing its working.

please what am I doing wrong?

TL;DR
User is seeking help with code on StackOverflow. Recommendation is to use async/await instead of .then for resolving promises. Code provided is difficult to read, and it is suggested to format it properly using backticks. User is tagging people and it is advised not to do so. User asks for clarification on what they are doing wrong with the code. Code provided is attempting to use Appwrite function to get a list of documents. Issue is that user is not getting any value returned. Solution: 1. Use async/await instead of .then for resolving promises. 2. Properly format the code using backticks. 3
Zionnite
13 Nov, 2023, 07:49

@Drake good day Sir,

Sir please can you make me understand what I'm doing Wrong on this code?

Mosh Ontong
13 Nov, 2023, 14:28

is this a cloud ? or self hosted?

Drake
13 Nov, 2023, 16:12

Please don't tag people as it can be very disruptive. You can post and wait for anyone to respond.

Drake
13 Nov, 2023, 16:13

FYI, it's best to wrap code in backticks to format a bit nicer. You can use 1 backtick for inline code (https://www.markdownguide.org/basic-syntax/#code) and 3 backticks for multiline code (https://www.markdownguide.org/extended-syntax/#syntax-highlighting.

Drake
13 Nov, 2023, 16:15

It's really hard to read the code, but the problem could be that the function is returning before the promise is resolved. I recommend not using promise.then. Instead, use async await.

Zionnite
13 Nov, 2023, 16:17
Zionnite
13 Nov, 2023, 20:22

Hello @Drake @Core @Mosh Ontong

I'm very aware everyone is on a busy schedule, can you please quickly take a look at what I'm doing wrong.

Thank you very much

Aditya Oberai
13 Nov, 2023, 20:25

@Zionnite let's not tag Core here please, especially since there are folks interacting in the thread

Steven has already responded on the StackOverflow question. Have you had a chance to review his answer and try the solution?

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