Variables syntax
davidbellerose
HOBBYOP

a year ago

When I create variables for my app, do I have to surround the key and value with back ticks ``, as shown in this doc?

https://docs.railway.app/reference/variables#template-syntax

0 Replies

brody
EMPLOYEE

a year ago

no you don't, the backticks are inline codeblocks


brody
EMPLOYEE

a year ago

n/a


davidbellerose
HOBBYOP

a year ago

Ok, my asp.net app for some reason can't read my variables, It can read the DATABASE_URL ok, but for some reason it can't read the others.


davidbellerose
HOBBYOP

a year ago

14162ece-686d-4a9e-804f-1f26e3774377


davidbellerose
HOBBYOP

a year ago

Are you able to check to see if I entered them correctly? I double checked all the keys and values in the railway variables and in my app to make sure they match.


brody
EMPLOYEE

a year ago

as im only a community mod i have zero access to your project, i would instead need to be shown screenshots and such


davidbellerose
HOBBYOP

a year ago

Here is an example

1260642564235726800


davidbellerose
HOBBYOP

a year ago

My deployment log says that the BaseUrl is nul


davidbellerose
HOBBYOP

a year ago

The syntax in my app to access the DATABASE_URL which works is the same to access the BaseUrl,


brody
EMPLOYEE

a year ago

okay, lets see the code side of things where you try to access BaseUrl


davidbellerose
HOBBYOP

a year ago

var baseUrl = _appSettings.TMDBSettings.BaseUrl ?? Environment.GetEnvironmentVariable("BaseUrl");

and here is the DATABASE_URL
var databaseUrl = Environment.GetEnvironmentVariable("DATABASE_URL");


davidbellerose
HOBBYOP

a year ago

And in another app I can access my email settings ok
var host = _mailSettings.MailHost ?? Environment.GetEnvironmentVariable("MailHost");


brody
EMPLOYEE

a year ago

and what error do you get


davidbellerose
HOBBYOP

a year ago

Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1] An unhandled exception has occurred while executing the request. System.NullReferenceException: Object reference not set to an instance of an object. at [MoviePro.Services](MoviePro.Services).TMDBMovieService.SearchMoviesAsync(MovieCategory category, Int32 count) in /app/Services/TMDBMovieService.cs:line 110


brody
EMPLOYEE

a year ago

those errors seem unrelated?


davidbellerose
HOBBYOP

a year ago

line 110 is the line I posted above for the BaseUrl, can you tell me how it is unrelated? I guess I misunterstood the error


brody
EMPLOYEE

a year ago

the error mentioned an int32 but environment variables are strings, this would be a code or config issue


davidbellerose
HOBBYOP

a year ago

Ah ok I missed that, sorry about that and thanks for pointing that out. 🙂


davidbellerose
HOBBYOP

a year ago

So the int32 is the count of records to retrieve from the api, and the debugger shows that it is passing into the method correctly, it is not a variable set in the Railway app. From what I can tell the lines of code I posted above, the BaseUrl for example is set as a string. So I'm still thinking that the app can't see the Railway variable for some reason


davidbellerose
HOBBYOP

a year ago

My app api works locally, but not on Railway


brody
EMPLOYEE

a year ago

are you using a dockerfile?


davidbellerose
HOBBYOP

a year ago

I don't believe so, It is .net mvc 6, I haven't had a need to create a docker file for my .net 6 apps. I did for my .net 8 blazor apps though.

I commented out this part in the line 110 I posted above just to test if the "??" part of the line isn't working. Redeploying now. I'll post the result in a few minutes


davidbellerose
HOBBYOP

a year ago

/*_appSettings.TMDBSettings.BaseUrl ??*/


davidbellerose
HOBBYOP

a year ago

lol that worked. I can see the api now. I'll make the adjustments to my code. Thanks for all your support and time. much appreciated


brody
EMPLOYEE

a year ago

no problem, glad you were able to solve!


davidbellerose
HOBBYOP

a year ago

So for anyone reading this I had to swap the two properties on either side of the "??" null-coalescing operator. For some reason the original first part _appSettings.TMDBSettings.BaseUrl , being local, should return null on Railway but somehow didn't. I assume it evaluated to something other than null, so the second part of the statement after the ?? was never evaluated. Swapping them allowed the Environment variable to be evaluated first, which returns null locally, but works on Railway since the Environment variable is evaluated first, and is successfully picked up on Railway by the app.

var baseUrl = Environment.GetEnvironmentVariable("BaseUrl") ?? _appSettings.TMDBSettings.BaseUrl;
At least that's my theory.


brody
EMPLOYEE

a year ago

thanks for the follow-up!


Loading...