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
- Function domain not available
Hello, even tho in docs you clearly describe that every function has its domain, I can not see it anywhere in any of my projects. How do I reveal the url of th...
- Inquiry: How to Reduce Cold Start Durati...
Hey! I was using Python for the function runtime, but after reading that Go has the fastest runtime, I switched my code over to Go. However, I'm still seeing co...
- After a GET request is passed to functio...
Create execution in the console can normally retrieve the get parameters。WHy?