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
- Unable to init function via cli
Hello! I have created a new self hosted instance of appwrite 1.9.6 and have connected to that insatnce with the cli 27.2.0 that was released a few hours ago. Wh...
- Appwrite-injected variables are not avai...
I am using rust-1.83 as a runtime and try to access APPWRITE_FUNCTION_API_ENDPOINT or APPWRITE_FUNCTION_API_KEY during runtime. Both are not set during my execu...
- OAuth Missing redirect URL
Hello all, on a Flutter mobile app with latest Appwrite dependency (25.4.0) I always got "Missing redirect URL" - "Your OAuth login flow is missing a proper red...