Back

[SOLVED] variables not readble to function

  • 0
  • Functions
  • Cloud
unsoop
17 Jul, 2023, 21:59

appwrite 1.3.7 with function python 3.10 on ubuntu arm.

is not able to read variables.

here is the message when uploading .env (jusit to prove i add vars)

no function data: the code i tryid for debugging:

TypeScript
def main(req, res):
    for key, value in os.environ.items():
        print(f"{key}: {value}")
    for var in req.variables:
        print(var)
        print(req.variables.get(var))
    print(os.getenv("SECRET_KEY"))
    print(req.variables.get('SECRET_KEY'))
    if not req.variables.get('_APP_SMTP_HOST'):
        return res.json({"success": False, "message": "missing GmailServer"})
    smtp_server = req.variables.get('_APP_SMTP_HOST')
    if not req.variables.get('_APP_SMTP_PORT'):
        return res.json({"success": False, "message": "missing GmailPort"})
    smtp_port = req.variables.get('_APP_SMTP_PORT')
    if not req.variables.get('_APP_SMTP_USERNAME'):
        return res.json({"success": False, "message": "missing UserId"})
    smtp_username = req.variables.get('_APP_SMTP_USERNAME')
    if not req.variables.get('_APP_SMTP_PASSWORD'):
        return res.json({"success": False, "message": "missing UserPassword"})
    smtp_password = req.variables.get('_APP_SMTP_PASSWORD')
    if not req.variables.get('ReciverEmail'):
        return res.json({"success": False, "message": "missing ReciverEmail"})
    receiver_email = req.variables.get('ReciverEmail'
TL;DR
Title: [SOLVED] Variables not readable in function The issue is that variables are not being read in the function code. The user tried various methods to access the variables, but none of them worked. They also mentioned that the issue occurs both in Python 3.9 and 3.10 as well as in their Dotnet function. Solution: - For Python, the correct formatting to access variables is `req.variables['YOUR_ENV_VAR']` instead of `req.variables.get('YOUR_ENV_VAR')`. - For Dotnet, the user can check if the `req.Variables` dictionary contains the variable before accessing
unsoop
17 Jul, 2023, 22:00

p.s you can change the vars to whatever...this was just 1 of the tests

unsoop
17 Jul, 2023, 22:10

example env file:

TypeScript
SMTP_HOST=smtp.gmail.com
SMTP_PORT=587
SMTP_USERNAME=someuser@bashans.net
SMTP_PASSWORD=adsomeKEY3wtbkekwy
RECIVER_EMAIL=ssomeReciver@gmail.com
D5
17 Jul, 2023, 22:15

Did you have tried adding the parameters manually?

D5
17 Jul, 2023, 22:16

Does it works that way?

D5
17 Jul, 2023, 22:17

Also the second screenshot doesn't seems to have an ENV var format

D5
17 Jul, 2023, 22:19

And also it seems you're not using the correct formatting. It should be like that:

TypeScript
req.variables['YOUR_ENV_VAR']
unsoop
17 Jul, 2023, 22:21

Even with python?

D5
17 Jul, 2023, 22:21

I think yes

D5
17 Jul, 2023, 22:21

According to docs

unsoop
17 Jul, 2023, 22:23

Here it uses "()"

D5
17 Jul, 2023, 22:58

Not sure then. In Dart

TypeScript
req.variables['YOUR_ENV_VAR']

seems to be working correctly. I think in python it should work too

unsoop
18 Jul, 2023, 06:19

so python 3.9 and 3.10 are not working

unsoop
18 Jul, 2023, 06:46

when i try to use dotnet

unsoop
18 Jul, 2023, 07:07

Dotnet also isnt resolving vars is set in the console

unsoop
18 Jul, 2023, 07:08
unsoop
18 Jul, 2023, 07:09

i followed this example: https://appwrite.io/docs/functions#writingYourOwnFunction

*the functioon runs, just vars are not working..

unsoop
18 Jul, 2023, 07:09
TypeScript
/*
  'req' variable has:
    'Headers' - object with request headers
    'Payload' - request body data as a string
    'variables' - object with function variables

  'res' variable has:
    'Send(text, status)' - function to return text response. Status code defaults to 200
    'Json(obj, status)' - function to return JSON response. Status code defaults to 200
  
  If an error is thrown, a response with code 500 will be returned.
*/

using System.Threading.Tasks;
using System.Collections.Generic;

public async Task<RuntimeResponse> Main(RuntimeRequest req, RuntimeResponse res)
{
    // Appwrite's SDK for .NET is currently under the works and shall be available soon
    var secretKey = req.Variables.ContainsKey("SECRET_KEY")
                ? req.Variables["SECRET_KEY"]
                : "SECRET_KEY variable not found. You can set it in Function settings.";
    Console.WriteLine(secretKey);
    return res.Json(new()
    {
        { "areDevelopersAwesome", true }
    });
}
unsoop
18 Jul, 2023, 07:13

@Eldad can some one help me 😦 it runs fine, just dosent get any variable i am setting.

unsoop
18 Jul, 2023, 07:26

when trying this dotnet example "as is" https://github.com/open-runtimes/examples/tree/main/dotnet/generateShortUrl

getting the fillwing return message:

Binyamin
18 Jul, 2023, 12:51

How did you run the function?

unsoop
18 Jul, 2023, 13:29

Just execute button in the ui

Binyamin
18 Jul, 2023, 13:29

And wait payload you've send?

unsoop
18 Jul, 2023, 13:30

Nothing

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