So Im having this issue where no error is thrown at all but my function isn't able to create documents inside my database.
import { Client, Databases, ID } from "node-appwrite";
export default async ({ req, res, log, error }) => {
if (req.method !== "POST")
return res.json({ message: "method not allowed", status: 405 }, 405);
const client = new Client()
.setEndpoint(process.env.APPWRITE_FUNCTION_API_ENDPOINT)
.setProject(process.env.APPWRITE_FUNCTION_PROJECT_ID)
.setKey(req.headers["x-api-key"]);
const databases = new Databases(client);
const body = req.bodyJson;
if (!body.level || !body.message || !body.timestamp)
return res.json({ message: "wrong log format", status: 400 }, 400);
const level = body.level.toLowerCase();
if (
level !== "info" &&
level !== "error" &&
level !== "debug" &&
level !== "warning" &&
level !== "notice" &&
level !== "crit" &&
level !== "alert" &&
level !== "emerg"
)
return res.json({ message: "invalid log level", status: 400 }, 400);
try {
await databases.createDocument(
process.env.DATABASE_ID,
process.env.COLLECTION_ID,
ID.unique(),
body
)
.then((result) => log(result.$id))
.catch((err) => error(err));
} catch (e) {
error(e);
return res.json({ message: "no access", status: 403 }, 403);
}
return res.empty();
};
Above is the function. I've given the key inside the header all permissions that I could. As I said there are no errors thrown or anything to indicate that the creation has failed but its not creating the document
What are you expecting to be in x-api-key, I don't think that's a supported header?...
Are you wanting the dynamic api key? If so thats x-appwrite-key
no, im expecting the request to have set that header. this header actually contains an api key. I tested if the key was actually received right in the function and it is. If I change the keys permissions in the console to have no database access it throws an error for insufficient permissions but if i give it access to database it shows no error. this leads me to think that it isnt a problem with the key
Probably not the issue but that try catch wouldn't actually make it to the catch block since you're catching it in your then.catch statement
What is the output of the createDocument? Nothing?
yes, i tired catching both ways with and without handling the promise. both no results
exactly, literally nothing
when printing the doc.$id it outputs undefined
Can you do this
try {
const res = await databases.createDocument(
process.env.DATABASE_ID,
process.env.COLLECTION_ID,
ID.unique(),
body
);
console.log(res);
} catch (e) {
console.log(e);
return res.text("error");
}
this document that is logged i added manually
this was the request
oh and the request returned with 204
yea 204 is no content, which is expected if it worked
so is your function listing documents too?
nope not supposed to
also didnt add anything to the db
is that data coming from the console.log after the createDocument function?
the console.log(res) must print the db's content then
thats what i thought too
wild that isn't right?
what version of node-appwrite
Recommended threads
- Error upgrading from 1.8.1 to 1.9.0
DO Self-hosted server failed to upgrade with this error "Error response from daemon: client version 1.52 is too new. Maximum supported API version is 1.42". U...
- MariaDB refuses to connect to appwrite
Earlier, I tried updating my Appwrite version from 18.1.x to the latest release because my Flutter package required it to function properly. I used the official...
- executeFunction intermittently throws Fo...
Environment: Flutter app using the Appwrite Flutter SDK, calling executeFunction for [describe endpoint, e.g. live-stream-related function]. *Description*: Int...