
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:
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:
...
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,
- How can I fix my function to find the Env Var?
- How can I tell VSCode to not give me type errors?
Kind regards, Marius

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.

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)

I just tried creating a new Node function and accessing an envvar with process.env.VAR_NAME
:
export default async ({ req, res, log, error }) => {
log(process.env.DEMO_ENV_VAR);
};

@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:
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.

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

@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.

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

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

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 π

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

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

so like
console.log("something logged " + withvariable); // This doesn't work for me usually
vs
console.log(`something logged ${withvariable}`);

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

[SOLVED] How to use variables in in functions correctly?
Recommended threads
- Get all Documents and Expanded Relations...
I have multiple (quite small) collections that have many-to-many relationships. How can we return all documents and expanded relationship for a specific collec...
- Functions Failing Due to Installed Depen...
Hi team! Before anything, I am on the so far while we're in the planning phase of the app, and will be upgrading to pro soon. If there's a possibility that this...
- The new Bulk API and Realtime updates
It seems that the new Bulk API doesn't actually send realtime updates which is making a huge problem for me. I also wanted to know different is it deleting mul...
