
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
- API preflight request not working on .f...
When I am calling a function on my APP through the domain is failing. Because the preflight request (OPTIONS HTTP request) times out. this only occurs with fu...
- No Headers
Hi I have 2 appwrite functions, one is working fine on localhost, second is not even working on prod with https, i inspected the api call, no headers are being ...
- Cloud Function Deployment crashing
My golang cloud function crashes on deployment and its a bit non-descript. Here is the error message. It says "Document already exists". This is a general probl...
