Railway unable to connect to linked database

I'm getting the following error when deploying my backend to railway.
[Nest] 26 - 10/08/2023, 3:37:23 AM ERROR [MongooseModule] Unable to connect to the database. Retrying (9)...

[Nest] 26 - 10/08/2023, 3:37:23 AM ERROR [ExceptionHandler] Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"
[Nest] 26 - 10/08/2023, 3:37:23 AM ERROR [MongooseModule] Unable to connect to the database. Retrying (9)...

[Nest] 26 - 10/08/2023, 3:37:23 AM ERROR [ExceptionHandler] Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"
It will run locally and connect to the database, but railway is unable to connect. Despite the .env file and the railway variables being the same. My connection string does start with "mongodb:// so I am unsure of where to start with this error.
No description
Solution:
Thanks for the help. I was able to figured out that railway is looking for MONGO_URL and my application was using MONGOURL locally.
Jump to solution
22 Replies
Percy
Percy8mo ago
Project ID: 8e9ef9bf-0cad-446a-a5fe-350cc41f3c87
Dylan-The-Villian
8e9ef9bf-0cad-446a-a5fe-350cc41f3c87
Brody
Brody8mo ago
do some simple debugging, like printing the environment variable
Dylan-The-Villian
It's printing fine in my routes, however I have noticed that my homepage of / is returning { "message": "Cannot GET /", "error": "Not Found", "statusCode": 404 } Could this be related?
Brody
Brody8mo ago
backend's don't typically have home pages? so no I don't see a problem with that
Dylan-The-Villian
That makes sense, I am new to this. I have all of my variables set up to be shared, is this the proper way to do this? All of the values match the local env file
No description
Brody
Brody8mo ago
why shared? you only have one service
Dylan-The-Villian
Nevermind they are service variables. I have deleted all of my shared variables just in case and restarted the deploy but it is the same issue,
Brody
Brody8mo ago
^
twizycat
twizycat8mo ago
twizycat
twizycat8mo ago
Nothing in my code changed today.. it just started crashing
Brody
Brody8mo ago
this error wouldn't be an issue with railway, please do some research on this topic, perhaps you are never closing your connections
Dylan-The-Villian
How can I print what the variables are on railway's end? I am able to print them locally but I need to see what railway is using for the connection string
Brody
Brody8mo ago
print what variables specifically?
Adam
Adam8mo ago
same as printing locally, just refer to the deployment logs
Dylan-The-Villian
The mongo_url I am seeing in the docs that I can use a console.log, but I am not seeing the results of it. Should I be looking in the build logs?
Brody
Brody8mo ago
the deployment logs but if you aren't seeing a log that may mean the variable is empty as railway trims leading and trailing whitespace and then if the resulting log line is empty nothing makes its way to the deployment logs so do console.log("mongo url", process.env.MONGO_URL) instead
Dylan-The-Villian
So i have this log in my main.ts file
await app.listen(port, "0.0.0.0", function () {
console.log("mongo url", process.env.MONGO_URL)
});;
await app.listen(port, "0.0.0.0", function () {
console.log("mongo url", process.env.MONGO_URL)
});;
And here is the full deploy log. I'm not seeing my log anywhere.
Brody
Brody8mo ago
it's crashing long before it gets to the point of starting the server, log that variable right before the database connection attempt, also, please do not use watch mode when on Railway, it can cause massive instabilities
Solution
Dylan-The-Villian
Thanks for the help. I was able to figured out that railway is looking for MONGO_URL and my application was using MONGOURL locally.
Brody
Brody8mo ago
awesome