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
- After assigning a domain to my Dart func...
I’ve attached the images. Could anyone please explain how this execution is being performed?
- I can't UNPAUSE my project with the free...
I received an email notifying me that my project had been paused due to inactivity, and the email included a link to "Restore project." However, that button red...
- Error trying to deploy functions from CL...
✗ Error • on-auth-create (6a00b2ab000c9ce2949b) • Server Error ✗ Error: No functions were pushed. Prject ID: 69ffe0270033a4ac420a