Hi, what could cause this problem? Code:
module.exports = async function (req, res) {
const client = new sdk.Client();
const database = new sdk.Databases(client);
const databaseId = "id";
const notificationsCollectionId = "id";
client
.setEndpoint(req.variables.APPWRITE_FUNCTION_API_ENDPOINT)
.setProject(req.variables.APPWRITE_FUNCTION_PROJECT_ID)
.setKey(req.variables.APPWRITE_FUNCTION_API_KEY);
try {
const payload = JSON.parse(req.payload);
const mentionedUsers = payload.mentions || [];
const message = payload.message || "You were mentioned in a post";
for (const userId of mentionedUsers) {
await database.createDocument(databaseId, notificationsCollectionId, sdk.ID.unique(), {
userId: userId,
message: message,
read: false,
createdAt: new Date().toISOString(),
});
}
res.json({ success: true, message: "Notifications created successfully." });
} catch (error) {
res.json({ success: false, message: error.message });
}
};```
could you send a screenshot of your executions tab?
Like this
i might got the problem with the log
it gets the log
but only the begining one
I think the problem maybe here:
for (const userId of mentionedUsers) {
You should be getting the log(mentionedUsers), so if it does fail there you should at least get this one. You may do something like this log(mentionedUsers: ${mentionedUsers})
ill try
nothingh
nothing is shown
actually, I don't think req.payload exists. It should be req.bodyJson, then mentionedUsers should be req.bodyJson['mentions']
const payload = JSON.parse(req.bodyJson['mentions']);
like this?
I don't think you need to parse it. I tshould already be parsed.
const payload = req.bodyJson;
const mentionedUsers = payload['mentions'] || [];
im trying it
I would log payload though.
const payload = req.bodyJson;
log(payload);
const mentionedUsers = payload['mentions'] || [];
i got it
it shows the mentions
cool cool, so it's working now?
yeah it created the documents, i just have to figure it out how to show this on vite
You can see what all is available on the request here https://appwrite.io/docs/products/functions/develop#request-types
[SOLVED] Functions: Cannot read properties of undefined (reading 'json')
thank you for your help!
Here's a tutorial for using Appwrite and react.
https://appwrite.io/docs/tutorials/react/step-1
Heres the other tutorials https://appwrite.io/docs/tutorials
Recommended threads
- 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...
- Functions in cloud console not works
please advise as all of my runing functions on Appwrite console was working before , but now it give this error [Invalid `headers` param: Value must be a valid...
- [SOLVED] Production down - createExecuti...
Hey, Since 1.9.6 rollout in fra (~17:00 UTC, 14 Aug), POST /v1/functions/{functionId}/executions returns a 400 whenever the body includes an empty headers ob...