Unable to load env from service variables
https://github.com/nytrek/bostadsval-backend
I have created a minimal repo that I have used to try and deploy to nestjs however the env does not appear to load in correctly. I'm using nestjs together with mongoose. When I wrap the env variable in strings I get the following error
- MongoParseError: Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"
and without strings
- MongoAPIError: URI cannot contain options with no value
which indicates to me that the service variable is loaded but not correctly. The minimal repo works 100% locally.
Please let me know if railway does not support this type of projects being deployed so that I can start looking for alternatives. Thanks
GitHub
GitHub - nytrek/bostadsval-backend
Contribute to nytrek/bostadsval-backend development by creating an account on GitHub.
Solution:Jump to solution
I solved it. It was a mixture of both user error (my part) and railway I would say. On vercel when you paste a env variable. You get both the name and value pasted. I thought it was the same here (see screen recording). It did work at first I thought but as i started to look closer I realized, my env was being cut. My connection string left out
?retryWrites=true&w=majority
when I pasted in my env variable25 Replies
Project ID:
0773ce81-f420-464d-9070-125dbbfff019
0773ce81-f420-464d-9070-125dbbfff019
you are storing the keys in the DOTENV locally?
@Kenny
and you are trying to bring that DOTENV file with your repo along onto railway correct?
Yes. So I have a .env.local file that i use to load in my variables locally. On railway I assumed that I needed to use the service variable. So i added my env variable manually - MONGODB_URI=MY_URI_STRING
You have them in here correct?
All your ENvs
Variables can be defined as simple key/value pairs or as Templated Variables (eg. ), which can dynamically reference other variables, shared variables, or plugin variables (more on this below).
can you show me the prefix of your mongodb uri?
you can blur out the rest just the prefix
like
mongodb://RESTOFTHEQUERY
or
mongodb+srv://RESTOFTHEQUERY
if you're trying to populate it with a normal http or https it wont work
This is how it looks in the raw editor on both tabs. I'm trying to reference the value in my project through ConfigModule.(nestjs) and directly process.env.MONGODB_URI -
1 second my js is not amazing let me sit down for this haha
ok i see you still have the local config.get etc etc in there for local dev
but since youre deploying now, id start out with removing that part
and ONLY referencing the mongodb_URI, also console.log it so u can see whether its getting loaded properly so u know if the railway side of things works
so
you can get rid of the import of the local file path, and the configservice too
try if this works and lmk
as it COULD again my js isnt rocket science, if your fallback isnt handled properly since you are checking for the local env first, it could fail in falling back to the system one
It did not work. I tried to log the values as you suggested to check if it was on railway's side. And it turns out the values are printed correctly. But somehow it's not being captured by the
MongooseModule
So the variables itself are not the problem correct?
Yes. It's very weird. I need to go and research why mongoose is not working as it should
Could it be theres some connections in the connection string
that are not assigned any value?
var1=var_value1;var2=;var3=var_value3
also i would temporarily hardcode your connection string and set your repository to private, to see whether it actually works
Yeah, hardcoding the connection string worked. So i'm really confused where it went wrong tbh
Also worked on railway or only local?
Worked on both
ok odd, if you're 100% sure the conn string itself works the only thing i can come up with is that Railway might add some empty spaces or different encoded characters to it like urls get encoded sometimes
i think its
to remove any trailing spaces or leading ones
then log it again and see if its the EXACT value that you know works
@Brody
Solution
I solved it. It was a mixture of both user error (my part) and railway I would say. On vercel when you paste a env variable. You get both the name and value pasted. I thought it was the same here (see screen recording). It did work at first I thought but as i started to look closer I realized, my env was being cut. My connection string left out
?retryWrites=true&w=majority
when I pasted in my env variableThank you for helping me @Morpheus
ahhh!!!
thats great to hear bro
im happy i could help
Yea its weird sometimes and it doesnt give the exact env like you specify it
@Brody can you mark this as solved?
you've been a big help, but please chill with the pings #🛂|readme #5
my bad im sorry man
all good, just keep that in mind 🙂