Cannot fetch a MongoDB collection.
Whenever I call my code to fetch a MongoDB collection, it results in a
MongooseError: Operation games.find()
buffering timed out after 10000ms
After a bit, this error pops up: MongooseServerSelectionError: getaddrinfo ENOTFOUND mongodb.railway.internal
Any idea why it might be? This is my code to fetch the data:
It does print "Connected to MongoDB" but then hangs and outputs the mentioned errors.
Any idea what might be up? Thank you.Solution:Jump to solution
the database name needs to be passed in as an option, not part of the url.
https://mongoosejs.com/docs/api/mongoose.html#Mongoose.prototype.connect()...
30 Replies
Project ID:
79c095c4-8db3-4df9-b281-a0313176f640
79c095c4-8db3-4df9-b281-a0313176f640
Update: Also happens during a connection sometimes.
the backend runs vite dev?
Yes
vite is a web bundler for frontend sites?
To put it into perspective, it is a +server.js in a SvelteKit app
I'm not sure I understand?
gotcha, didn't know it was a sveletkit app
you need to run the built files instead of a development server
let's fix the bigger issue first
^
After running
npm run build (vite build)
npm run preview (vite preview)
please do not run a development server
I'm testing it locally with the two commands I sent, which I assumed make it run in a prod environment.
In that case, could you please clarify what you mean by "development server"?
vite dev
dev is short for development
Yes, but the last screenshot I sent is after running in
vite preview
vite preview is the same thing as vite dev
on Railway you do not want to run a development server
Oh, I didn't know that. Sorry, I haven't worked that much with Vite before and this is what I found it the docs regarding the build process.
please update your start command to run the built file
Ok, I'll try to do it, but I'm not exactly sure what the correct approach is
Or rather I don't entirely know what the output of the build is
run the build locally and look
I see the output/server and output/client with files in it, but I don't know how that connects to Railway, or more specifically which commands run that
Because as in all docs I found so far, prod env is apparently ran by vite preview.
vite preview is a development server.
you need to update your start script to run the built server.
Alright, I fixed it by adding the node adapter and npm run start to be
node build/index.js
I am having issues with the app not responding on Railway, so I'll have to fix the port real quick
Actually, no, that seems fine... the default is set to 3000, and on Railway, it runs at 8080, presumably something provided by the $PORT env variable.
Oh yeah, after providing a $PORT env variable to a random number, it runs correctly on the specific port
Any idea why the app might not be responding then?
Custom port provided, oh god, did not notice that...wrong target port most likely
Yes, I accidentally had an overridden target port
Alright, so that is working.
This was a fix of the wrong environment, but it should not affect the database problem I had, right?
Because I'm still getting the same error 😅
Or rather on a local node build/index.js the same error, on Railway I get
MongoServerError: Authentication failed.
locally you can't use the internal domain
as for the error on Railway, you are using Mongoose wrong
Solution
the database name needs to be passed in as an option, not part of the url.
https://mongoosejs.com/docs/api/mongoose.html#Mongoose.prototype.connect()
Isn't this what I am doing and what is also in the docs?
the docs I linked says otherwise
please look at the
dbName
optionOkay, I see now