Back

TypeError: Cannot read properties of undefined

  • 0
  • Functions
THE-E
3 May, 2023, 19:51

Yes, there was a restart between the errors.

TL;DR
The user encountered a TypeError: Cannot read properties of undefined error, but it seems to have been fixed after a restart. The user suggests scrutinizing the error as it may cause unexpected downtime in production. The support team suggests updating the code and checking the docker logs for any errors/warnings. However, it is unclear if the suggested solution resolved the issue.
Drake
3 May, 2023, 19:52

maybe try redeploying?

THE-E
3 May, 2023, 19:53

The function has been updated and adjusted for 1.3.0. But the result is the same :/

Drake
3 May, 2023, 19:54

Have you tried deleting the variables and adding them back?

THE-E
3 May, 2023, 19:57

Just tried that, but has no impact.

THE-E
3 May, 2023, 20:02

Can I be sure, that the error is located within the read of the environment variable or can it also be an issue on the MongoDB side?

THE-E
3 May, 2023, 20:10

I just looked for a function, where I didn't use the MONGODB_USER. I get the same error for an other environment variable with the same error message. So in conclusion it shouldn't be related to the MongoDB connection.

Drake
3 May, 2023, 20:35

The stack trace seems to indicate it's req.variables problem.

Drake
3 May, 2023, 20:35

What's docker ps show?

THE-E
3 May, 2023, 21:08

What exactly should be expected? The Function Container are listed.

Drake
3 May, 2023, 21:09

all the appwrite containers and their versions...

THE-E
3 May, 2023, 21:11
Drake
3 May, 2023, 21:13

would you please make sure to show the container names?

THE-E
3 May, 2023, 21:14

Hope it is readible

Drake
3 May, 2023, 21:18

can you check the docker logs for the appwrite-executor and appwrite-worker-functions containers to see if there are any errors/warnings?

THE-E
3 May, 2023, 21:21

appwrite-executor:

Drake
3 May, 2023, 21:21

could you also try updating your code to do this at the beginning:

TypeScript
res.json(req);
return;
THE-E
3 May, 2023, 21:22

docker logs 630f8d82d13133b826cc-6452b553edb419672a03

THE-E
3 May, 2023, 21:28
TypeScript
import { Client, Account } from 'node-appwrite';

module.exports = async function (req: any, res: any) {
  const client = new Client();

  if (!req.variables['APPWRITE_FUNCTION_ENDPOINT'] || !req.variables['APPWRITE_FUNCTION_API_KEY']) {
    console.warn('Environment variables are not set. Function cannot use Appwrite SDK.');
  } else {
    client
      .setEndpoint(req.variables['APPWRITE_FUNCTION_ENDPOINT'])
      .setProject(req.variables['APPWRITE_FUNCTION_PROJECT_ID'])
      .setJWT(req.variables['APPWRITE_FUNCTION_JWT'])
      .setSelfSigned();
  }
  const account = new Account(client);

  let promise = account.get();
  promise.then(
    function (response) {
      let userId = response.$id;
    },
    function (error) {
      console.log(error); // Failure
    }
  );

  async function run() {
    try {
      //Create Response
      res.json(req);
      return;
    } catch (error) {
      console.log('Error parsing JSON:', error, req.payload);
    } finally {
    }
  }
  run().catch(console.dir);
};

Is this sufficient (after compiling to JS)?

Drake
3 May, 2023, 21:35

ideally, you would compile and deploy:

TypeScript
module.exports = async function (req: any, res: any) {
  res.json(req);
}

in the same function as your previous one

Drake
3 May, 2023, 23:38

I think i found the problem...

Drake
3 May, 2023, 23:38

let me see how fast we can fix this

THE-E
4 May, 2023, 12:26

I tried today your minimal function. It worked fine. After that all the functions worked fine as well. There was another restart between the tests, so I am not sure what cured the error. I am unable to reproduce my error, but for now it works fine again. It would be nice, if the error could be scrutinized, because otherwise my app would have unexpected downtime, once I go to production.

Drake
4 May, 2023, 13:32

Ya...sorry, we had a problem with our runtime image

THE-E
4 May, 2023, 14:03

No reason to be sorry about.πŸ˜‰ I very much appreciate the dedication you guys put into the project and also your quick help and support. Thanks a lot for that! <:appwriteheart2:1073243188954935387> πŸ™‚

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