Skip to content
Back

[SOLVED] Function Errors

  • 0
  • Functions
sprouts
14 May, 2023, 20:46

Hi i have a function that should create a team. The function triggers correctly. And the loggs show this. Trying to create team for user 15907099529 with name FriendsOf-Test Error creating team for user 15907099529 with name FriendsOf-Test {}

Why to the logs not show what the error is. I am a bit stuck on that to do. I have included my code for help.

TypeScript
try {
    console.log(`Trying to create team for user ${UserId} with name-${UserId}`);
    const teamCreateResponse = await teams.create(UserId, UserId);     
    console.log("Team created successfully!");
    console.log(teamCreateResponse);
    res.json({
      success: true,
      response: teamCreateResponse,
    });
  } catch (teamErrorResponse) {
    console.log(`Error creating team for user ${UserId} with name-${UserId}`);
    console.log(teamErrorResponse);
    res.json({
      success: false,
      response: teamErrorResponse,
    });
  }
TL;DR
The user was experiencing function errors but was able to solve one of them by changing the APPWRITE_FUNCTION_ENDPOINT to the local IP. However, they noticed that the users.*.create trigger was only working with default signup and not with oAuth. As a workaround, they used the users.*.sessions.*.create trigger instead. They also mentioned that they were going to upgrade to version 1.3.4. The user had another issue where the Docker container was not able to reach Appwrite, resulting in an ECONNREFUSED error. They shared their code and sought help in understanding why the error was not displayed in the logs
Drake
14 May, 2023, 21:06

Btw, it's best to use 3 back ticks with multi-line code. See https://www.markdownguide.org/extended-syntax/#syntax-highlighting

Drake
14 May, 2023, 21:06

Try to wrap the error with str()

sprouts
14 May, 2023, 21:40

I have tried but i dont think its initialised.

TypeScript
   console.log(`Error- ${JSON.stringify(teamErrorResponse)}`);
sprouts
14 May, 2023, 21:45

I have `thrown the error now with this

TypeScript
throw new Error(`Error- ${JSON.stringify(teamErrorResponse)}`);

and i get

TypeScript
Error: Error- {}
    at module.exports (/usr/code-start/src/index.js:92:11)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async /usr/local/src/server.js:68:13
Drake
15 May, 2023, 00:39

Make sure your code doesn't throw any exceptions.

Drake
15 May, 2023, 00:40

Don't stringify the error like that. Just str(error)

sprouts
15 May, 2023, 09:31
TypeScript
} catch (teamErrorResponse) {
    console.log(`Error creating team for user ${UserId} with name-${UserId}`);
    console.log(teamErrorResponse);
    console.log(str(teamErrorResponse));
    res.json({
      success: false,
      response: teamErrorResponse,
    });
  }

I have tried like you suggested and added str but this is not defined

ReferenceError: str is not defined at module.exports (/usr/code-start/src/index.js:94:13) at processTicksAndRejections (node:internal/process/task_queues:96:5) at async /usr/local/src/server.js:68:13

sprouts
15 May, 2023, 09:31
TypeScript
} catch (teamErrorResponse) {
    console.log(`Error creating team for user ${UserId} with name-${UserId}`);
    console.log(teamErrorResponse);
    console.log(String(teamErrorResponse));
    res.json({
      success: false,
      response: teamErrorResponse,
    });
  }

I have tried altering your suggestion and added String(teamErrorResponse) This get me an error.

Error: connect ECONNREFUSED 127.0.0.1:80

sprouts
15 May, 2023, 09:34

This is very odd to me as when i do a simulated function call with.. "test": "node src/test.js",

test.js (test only API key and user data)

sprouts
15 May, 2023, 09:34
TypeScript
const sdk = require("node-appwrite");
const testPoint = require("./index.js");

let req = {
    "variables": {
        "APPWRITE_FUNCTION_ENDPOINT": "http://localhost/v1",
        "APPWRITE_FUNCTION_PROJECT_ID": "637fb4a92e46b8811264",
        "APPWRITE_FUNCTION_API_KEY": "TESTKEY",
        "APPWRITE_FUNCTION_ID": "645ea70bd2c181675e41",
        "APPWRITE_FUNCTION_NAME": "NewUserTeam",
        "APPWRITE_FUNCTION_DEPLOYMENT": "6461f70104ad3cd03ffe",
        "APPWRITE_FUNCTION_TRIGGER": "event",
        "APPWRITE_FUNCTION_RUNTIME_NAME": "Node.js",
        "APPWRITE_FUNCTION_RUNTIME_VERSION": "16.0",
        "APPWRITE_FUNCTION_EVENT": "users.6461f74228b337eba672.create",
        "APPWRITE_FUNCTION_EVENT_DATA": "{\"$id\":\"6461f74228b337eba672\",\"$createdAt\":\"2023-05-15T09:11:30.235+00:00\",\"$updatedAt\":\"2023-05-15T09:11:30.235+00:00\",\"name\":\"Test USer\",\"password\":\"$argon2id$v=19$m=65536,t=4,p=3$elBkd0VKNWJlYXBXQ1pYUQ$LCHJpApaPHGe3gK\\/6Hqpg5y9KA25viQsvz4JFdfppOY\",\"hash\":\"argon2\",\"hashOptions\":{\"type\":\"argon2\",\"memoryCost\":2048,\"timeCost\":4,\"threads\":3},\"registration\":\"2023-05-15T09:11:30.235+00:00\",\"status\":true,\"passwordUpdate\":\"2023-05-15T09:11:30.235+00:00\",\"email\":\"test.user@testing.com\",\"phone\":\"+34234324\",\"emailVerification\":false,\"phoneVerification\":false,\"prefs\":[]}",
        "APPWRITE_FUNCTION_DATA": "",
        "APPWRITE_FUNCTION_USER_ID": "642a96237048b738abbe",
        "APPWRITE_FUNCTION_JWT": ""
    },
    "headers": {},
    "payload": ""
}

let res = { json: (data) => { console.log(data); } };
testPoint(req, res);
sprouts
15 May, 2023, 09:36

The code runs no problem with running the index.js from my host. As connection to appwrite backed in esablished and function finishes its work. The only difference i can think of is the docker container not being able to reach appwrite. Something there resulting in **Error: connect ECONNREFUSED 127.0.0.1:80 **

Drake
15 May, 2023, 17:45

God sorry...I thought you were using python for some reason. It would be error.toString() in JavaScript

sprouts
15 May, 2023, 17:46

No worries i appreciate any help i can get 🙂

Drake
15 May, 2023, 17:46

You can't use localhost in functions because the runtime container will try to connect to itself rather than Appwrite. You can try using the LAN IP of your machine if you're developing locally

sprouts
15 May, 2023, 17:47

Ok so change my APPWRITE_FUNCTION_ENDPOINT variable to my local lan ip?

sprouts
15 May, 2023, 17:49

I will give that a go. Thankyou. I am going through the docs to upgrade to 1.3.4 now but will update when findings when done.

sprouts
15 May, 2023, 19:30

Thankyou @Steven changing the APPWRITE_FUNCTION_ENDPOINT to the local ip worked. Thanks a lot. Did notice however that the users..create trigger was working ONLY with default signup but not from oAuth as described in Issue 5198 . Using the users..sessions..create as workaround for now.

sprouts
15 May, 2023, 19:32

[SOLVED] Function Errors

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