Cannot fetch a MongoDB collection.
sebight
HOBBYOP

2 years ago

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:

import axios from "axios";
import mongoose from "mongoose";
import { MONGO_URL } from '$env/static/private'

const gameSchema = new mongoose.Schema({
    name: String
  });

export async function GET({request}) {
    console.log("GET /api/games");
    await mongoose.connect(MONGO_URL + "/test2");
    const Game = mongoose.model("Game", gameSchema);
    console.log("Connected to MongoDB");

    try {
        const gameList = await Game.find({});
        console.log(gameList);

        return new Response(JSON.stringify(gameList), {
            status: 200,
            headers: {
                "Content-Type": "application/json",
            },
        });
    } catch (error) {
        console.error("Error fetching games:", error);
        return new Response("Error fetching data", {
            status: 500,
            headers: {
                "Content-Type": "application/json",
            },
        });
    } finally {
        mongoose.connection.close();
    }
};

It does print "Connected to MongoDB" but then hangs and outputs the mentioned errors.

Any idea what might be up? Thank you.

Closed

48 Replies

sebight
HOBBYOP

2 years ago

79c095c4-8db3-4df9-b281-a0313176f640


sebight
HOBBYOP

2 years ago

Update: Also happens during a connection sometimes.


2 years ago

the backend runs vite dev?


sebight
HOBBYOP

2 years ago

Yes


2 years ago

vite is a web bundler for frontend sites?


sebight
HOBBYOP

2 years ago

To put it into perspective, it is a +server.js in a SvelteKit app


sebight
HOBBYOP

2 years ago

I'm not sure I understand?


2 years ago

gotcha, didn't know it was a sveletkit app


2 years ago

you need to run the built files instead of a development server


sebight
HOBBYOP

2 years ago

1301968884517048371

1301968884756381726


2 years ago

let's fix the bigger issue first


2 years ago

^


sebight
HOBBYOP

2 years ago

After running

npm run build (vite build)

npm run preview (vite preview)


2 years ago

please do not run a development server


sebight
HOBBYOP

2 years ago

I'm testing it locally with the two commands I sent, which I assumed make it run in a prod environment.


sebight
HOBBYOP

2 years ago

In that case, could you please clarify what you mean by "development server"?


2 years ago

vite dev

dev is short for development


sebight
HOBBYOP

2 years ago

Yes, but the last screenshot I sent is after running in vite preview


2 years ago

vite preview is the same thing as vite dev


2 years ago

on Railway you do not want to run a development server


sebight
HOBBYOP

2 years ago

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.


2 years ago

please update your start command to run the built file


sebight
HOBBYOP

2 years ago

Ok, I'll try to do it, but I'm not exactly sure what the correct approach is


sebight
HOBBYOP

2 years ago

Or rather I don't entirely know what the output of the build is


2 years ago

run the build locally and look


sebight
HOBBYOP

2 years ago

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


sebight
HOBBYOP

2 years ago

Because as in all docs I found so far, prod env is apparently ran by vite preview.


2 years ago

vite preview is a development server.

you need to update your start script to run the built server.


sebight
HOBBYOP

2 years ago

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


sebight
HOBBYOP

2 years ago

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.


sebight
HOBBYOP

2 years ago

Oh yeah, after providing a $PORT env variable to a random number, it runs correctly on the specific port


sebight
HOBBYOP

2 years ago

Any idea why the app might not be responding then?


sebight
HOBBYOP

2 years ago

Custom port provided, oh god, did not notice that...


2 years ago

wrong target port most likely


sebight
HOBBYOP

2 years ago

Yes, I accidentally had an overridden target port


sebight
HOBBYOP

2 years ago

Alright, so that is working.


sebight
HOBBYOP

2 years ago

This was a fix of the wrong environment, but it should not affect the database problem I had, right?


sebight
HOBBYOP

2 years ago

Because I'm still getting the same error 😅


sebight
HOBBYOP

2 years ago

Or rather on a local node build/index.js the same error, on Railway I get MongoServerError: Authentication failed.


2 years ago

locally you can't use the internal domain


2 years ago

as for the error on Railway, you are using Mongoose wrong


2 years ago

the database name needs to be passed in as an option, not part of the url.


sebight
HOBBYOP

2 years ago

1301980176766472212


sebight
HOBBYOP

2 years ago

Isn't this what I am doing and what is also in the docs?


2 years ago

the docs I linked says otherwise


2 years ago

please look at the dbName option


sebight
HOBBYOP

2 years ago

1301980707429552138


sebight
HOBBYOP

2 years ago

Okay, I see nw


Welcome!

Sign in to your Railway account to join the conversation.

Loading...