Skip to content
Back

[SOLVED] Functions: Cannot read properties of undefined (reading 'json')

  • 0
  • Functions
zombikaa
19 Nov, 2024, 08:17

Hi, what could cause this problem? Code:

TypeScript
 
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 });
    }
};```
TL;DR
Developers had trouble using functions with Appwrite and React due to errors reading properties. After analyzing their code snippets and troubleshooting, they discovered issues with parsing `req.bodyJson`, missing return statements, and incorrect function structures. The solution included proper syntax for handling `req.bodyJson`, validating response returns, and updating the package.json file with `"type":"commonjs"`. Additionally, they needed to adapt to Appwrite's context object parameter.
Kenny
19 Nov, 2024, 16:48

could you send a screenshot of your executions tab?

Kenny
19 Nov, 2024, 16:48

Like this

zombikaa
19 Nov, 2024, 16:49

i might got the problem with the log

zombikaa
19 Nov, 2024, 16:50

it gets the log

zombikaa
19 Nov, 2024, 16:50

but only the begining one

zombikaa
19 Nov, 2024, 16:50

I think the problem maybe here:

for (const userId of mentionedUsers) {

Kenny
19 Nov, 2024, 16:53

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})

zombikaa
19 Nov, 2024, 16:54

ill try

zombikaa
19 Nov, 2024, 16:54

nothingh

zombikaa
19 Nov, 2024, 16:55

nothing is shown

Kenny
19 Nov, 2024, 16:55

actually, I don't think req.payload exists. It should be req.bodyJson, then mentionedUsers should be req.bodyJson['mentions']

zombikaa
19 Nov, 2024, 16:56

const payload = JSON.parse(req.bodyJson['mentions']);

zombikaa
19 Nov, 2024, 16:56

like this?

Kenny
19 Nov, 2024, 16:57

I don't think you need to parse it. I tshould already be parsed.

TypeScript
const payload = req.bodyJson;
const mentionedUsers = payload['mentions'] || [];
zombikaa
19 Nov, 2024, 16:57

im trying it

Kenny
19 Nov, 2024, 16:57

I would log payload though.

TypeScript
const payload = req.bodyJson;
log(payload);
const mentionedUsers = payload['mentions'] || [];
zombikaa
19 Nov, 2024, 16:58

i got it

zombikaa
19 Nov, 2024, 16:58

it shows the mentions

Kenny
19 Nov, 2024, 16:58

cool cool, so it's working now?

zombikaa
19 Nov, 2024, 16:59

yeah it created the documents, i just have to figure it out how to show this on vite

Kenny
19 Nov, 2024, 16:59

You can see what all is available on the request here https://appwrite.io/docs/products/functions/develop#request-types

Kenny
19 Nov, 2024, 16:59

[SOLVED] Functions: Cannot read properties of undefined (reading 'json')

zombikaa
19 Nov, 2024, 16:59

thank you for your help!

Kenny
19 Nov, 2024, 17:00

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

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more