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
- Functions Failing on CRON Schedule
I set up an hourly CRON Schedule for my function and It only executes successfully every after 3 hours sometimes 10 hours. When I execute it manually it works e...
- MongoDb Database Breaks integer[] & bigi...
I've got a table with the below column created. When I try to insert the value `[-3408048000]` into it, the dart sdk cloud function call is successful (no error...
- Appwrite Functions cold start
Hello. I'm seeing really long cold starts on Appwrite Cloud Functions and wanted to know if this is expected. My function just authenticates the user, fetches ...