R
Railway•14mo ago
Kenny

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:
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 variable
Jump to solution
25 Replies
Percy
Percy•14mo ago
Project ID: 0773ce81-f420-464d-9070-125dbbfff019
Kenny
Kenny•14mo ago
0773ce81-f420-464d-9070-125dbbfff019
Floris
Floris•14mo ago
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?
Kenny
Kenny•14mo ago
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
Floris
Floris•14mo ago
No description
Floris
Floris•14mo ago
You have them in here correct? All your ENvs
Kenny
Kenny•14mo ago
No description
Floris
Floris•14mo ago
Floris
Floris•14mo ago
Variables can be defined as simple key/value pairs or as Templated Variables (eg.
${{Postgres.DATABASE_URL}}
${{Postgres.DATABASE_URL}}
), 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
Kenny
Kenny•14mo ago
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 -
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { MongooseModule } from '@nestjs/mongoose';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { Listing, ListingSchema } from './schemas/listing.schema';

@Module({
imports: [
ConfigModule.forRoot({
envFilePath: '.env.local',
}),
//https://stackoverflow.com/questions/72460269/how-to-read-a-env-file-on-mongoosemodule-in-nestjs
MongooseModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (config: ConfigService) => ({
uri: config.get<string>('MONGODB_URI') || process.env.MONGODB_URI, // Loaded from .ENV
dbName: 'listings',
}),
}),
MongooseModule.forFeature([
{ name: Listing.name, schema: ListingSchema, collection: 'listings' },
]),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { MongooseModule } from '@nestjs/mongoose';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { Listing, ListingSchema } from './schemas/listing.schema';

@Module({
imports: [
ConfigModule.forRoot({
envFilePath: '.env.local',
}),
//https://stackoverflow.com/questions/72460269/how-to-read-a-env-file-on-mongoosemodule-in-nestjs
MongooseModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (config: ConfigService) => ({
uri: config.get<string>('MONGODB_URI') || process.env.MONGODB_URI, // Loaded from .ENV
dbName: 'listings',
}),
}),
MongooseModule.forFeature([
{ name: Listing.name, schema: ListingSchema, collection: 'listings' },
]),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
No description
No description
Floris
Floris•14mo ago
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
// rest stays the same
uri: process.env.MONGODB_URI,
// rest of code
// rest stays the same
uri: process.env.MONGODB_URI,
// rest of code
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { MongooseModule } from '@nestjs/mongoose';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { Listing, ListingSchema } from './schemas/listing.schema';

@Module({
imports: [
ConfigModule.forRoot(),
MongooseModule.forRootAsync({
useFactory: async () => ({
uri: process.env.MONGODB_URI, // railway loaded from system environment variables
dbName: 'listings',
}),
}),
MongooseModule.forFeature([
{ name: Listing.name, schema: ListingSchema, collection: 'listings' },
]),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { MongooseModule } from '@nestjs/mongoose';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { Listing, ListingSchema } from './schemas/listing.schema';

@Module({
imports: [
ConfigModule.forRoot(),
MongooseModule.forRootAsync({
useFactory: async () => ({
uri: process.env.MONGODB_URI, // railway loaded from system environment variables
dbName: 'listings',
}),
}),
MongooseModule.forFeature([
{ name: Listing.name, schema: ListingSchema, collection: 'listings' },
]),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
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
Kenny
Kenny•14mo ago
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
Floris
Floris•14mo ago
So the variables itself are not the problem correct?
Kenny
Kenny•14mo ago
Yes. It's very weird. I need to go and research why mongoose is not working as it should
Floris
Floris•14mo ago
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
Kenny
Kenny•14mo ago
Yeah, hardcoding the connection string worked. So i'm really confused where it went wrong tbh
Floris
Floris•14mo ago
Also worked on railway or only local?
Kenny
Kenny•14mo ago
Worked on both
Floris
Floris•14mo ago
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
.trim()
.trim()
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
Kenny
Kenny•14mo ago
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 variable
Kenny
Kenny•14mo ago
Thank you for helping me @Morpheus
Floris
Floris•14mo ago
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?
Brody
Brody•14mo ago
you've been a big help, but please chill with the pings #🛂|readme #5
Floris
Floris•14mo ago
my bad im sorry man
Brody
Brody•14mo ago
all good, just keep that in mind 🙂
Want results from more Discord servers?
Add your server