Back

[SOLVED] How to use variables in in functions correctly?

  • 0
  • Functions
  • Cloud
Marius Bolik
2 Nov, 2023, 14:00

In the new functions, req.variables['APPWRITE_FUNCTION_API_KEY'] doesn't work anymore. So I switched to the new syntax. But I still get the following error:

TypeScript
TypeError: Cannot read properties of undefined (reading 'APPWRITE_FUNCTION_ENDPOINT')
    at /usr/local/server/src/function/src/index.js:19:31

The ENV Var is added in the Console. This is my code:

TypeScript
... 
if (
 !({}).APPWRITE_FUNCTION_ENDPOINT ||
 !({}).APPWRITE_FUNCTION_API_KEY
) { return ... }
...

This also gives my some type errors in my IDE: Property 'APPWRITE_FUNCTION_ENDPOINT' does not exist on type '{}'.

Considering that Functions are supposed to support Typescript soon, I find the current implementation a bit questionable.

So,

  1. How can I fix my function to find the Env Var?
  2. How can I tell VSCode to not give me type errors?

Kind regards, Marius

TL;DR
Issue: The user is having trouble accessing environment variables in their JavaScript function and is also getting type errors in their IDE. Solution: 1. To fix the issue with accessing environment variables, use the correct syntax: `process.env.VAR_NAME`. 2. To prevent type errors in VSCode, declare the environment variables using a `declare` statement or install a package like `dotenv` to handle the type definitions. Note: The user also mentions that logging with concatenated strings can cause issues and suggests using template literals (`console.log(`something logged ${withvariable}`)`) instead.
ideclon
2 Nov, 2023, 14:25

Your envvars in JS should be at process.env.VAR_NAME.

I’ve not seen the syntax shown in the docs before - it looks like it’s just creating an empty Object.

Marius Bolik
2 Nov, 2023, 14:36

Hi @ideclon, I also can't access the vars in process.env. It's nothing there. You can see the syntax here: https://appwrite.io/docs/products/functions/development#life-cycle And here: https://appwrite.io/docs/products/functions/examples (in the second code box)

ideclon
2 Nov, 2023, 14:45

I just tried creating a new Node function and accessing an envvar with process.env.VAR_NAME:

TypeScript
export default async ({ req, res, log, error }) => {
  log(process.env.DEMO_ENV_VAR);
};
Marius Bolik
2 Nov, 2023, 14:51

@ideclon You're right! The env vars seem to be there, but I can't see them in the logs. If I run this Code: log('VAR: ' + process.env.APPWRITE_FUNCTION_ENDPOINT);, it outputs VAR: APPWRITE_FUNCTION_ENDPOINT

But if I run this code:

TypeScript
if (process.env.APPWRITE_FUNCTION_PROJECT_ID === 'my-test-project') {
    log('COULD READ ENV VARS');
  } else {
    log('COULD NOT READ ENV VARS')
  }

It outputs COULD READ ENV VARS. Seems like I can't see the vars through security reasons or so.

Guille
2 Nov, 2023, 15:04

Does log(process.env) gives you any output?

Marius Bolik
2 Nov, 2023, 15:45

@Guille Somehow this gives me the entire process object. But log(process.env.APPWRITE_FUNCTION_PROJECT_ID) without any extra strings gives me the correct value. And my vars are also included in the root level of the process object.

Drake
2 Nov, 2023, 16:43

it looks fine to me 🧐 can you try clearing your cache?

Drake
2 Nov, 2023, 16:44

weird πŸ‘€ maybe process was reassigned? Can you share your full code?

Marius Bolik
2 Nov, 2023, 17:25

Website has now updated for me! Even without deleting the browser cache. I never reassigned the process env var. @Steven But as long as the env vars are working for me everything is fine for me. Thank you πŸ™‚

Drake
2 Nov, 2023, 18:03

ok so everything good now? can this be marked as solved?

ZachHandley
2 Nov, 2023, 19:24

one thing to note, if you are logging things, I've found that unless it's one string consistently it gets mad with concatenation for some reason

ZachHandley
2 Nov, 2023, 19:25

so like

TypeScript
console.log("something logged " + withvariable); // This doesn't work for me usually
vs
console.log(`something logged ${withvariable}`);
Marius Bolik
6 Nov, 2023, 11:29

Thank you @ZachHandley & @Steven ! This can now be marked as solved.

ZachHandley
6 Nov, 2023, 16:10

[SOLVED] How to use variables in in functions correctly?

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