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?
The problem could be that the function is returning before the promise is resolved. I recommend not using
promise.then
. Instead, use async await.So, this block:
would be: