[SOLVED] [Node, Koa, MongoDB] Unable to retrieve Document internally
preston206
HOBBYOP

2 years ago

I have a Node app, which uses the Koa framework and connects to a MongoDB database.

When I am working locally, I use the public URL and I have a condition that will use the internal URL on the Railway side. Like this:

const dbUrl = process.env.NODE_ENV === "development" ? process.env.MONGO_PUBLIC_URL : process.env.MONGO_URL;

console.log('---DB URL', dbUrl);

With that console log I can see that the internal URL is being used when I check my Railway logs.

And then I have some code that will connect to the DB and retrieve one document. It works locally, but not internally.

const dbClient = new MongoClient(dbUrl);
const dbName = process.env.DATABASE_NAME;

let collectionDocuments; // collection name: 'documents'

dbClient.connect()
  .then(conn => conn)
  .then(client => client.db(dbName))
  .then(db => {
    collectionDocuments = db.collection('documents');
    console.log('---connected to the database server and database');
  })
  .catch(error => console.error('---CONNECTION ERROR: ', error))

// koa router - get endpoint
router
  .get('/api/test', async (ctx) => {
    let doc;
    try {
      doc = await collectionDocuments.findOne({ "name": "test" });
    }
    catch (error) {
      console.log('---MONGO DB FIND ERROR: ', error);
    }

    console.log('---DOC', doc);

    ctx.status = 200;
    ctx.body = { message: 'Hello from Koa!' };
  })

Locally, I get this:

---DOC {
  _id: new ObjectId('66c54855698e75896efa6c59'),
  name: 'test',
  description: 'Lorem ipsum placeholder text.'
}

My Railway logs show this:

---DOC null

In my Node service variables, I have MONGO_URL .

Any ideas?

9 Replies

preston206
HOBBYOP

2 years ago

bump


2 years ago

Please provide any errors you are getting.


preston206
HOBBYOP

2 years ago

hi, thanks for replying. I just restarted my Node service and this is the entire log during the restart:

npm ERR! path /app

npm ERR! command failed

npm ERR! signal SIGTERM

npm ERR! command sh -c node index.js

npm ERR! A complete log of this run can be found in: /root/.npm/_logs/2024-08-23T20_51_09_308Z-debug-0.log

npm WARN config production Use --omit=dev instead.

> koa-preact-google-run-1@1.0.0 start

> node index.js

---ENV production

---DB URL mongodb://mongo:wdIJPVxxxxxxxxxxxxxxxxxxxisy@mongodb.railway.internal:27017

Server listening on port 8080

---connected to the database server and database

Interrupted operation as its client disconnected

Connection ended

Connection ended

Connection ended

Connection accepted

client metadata

Connection accepted

client metadata

Auth metrics report

Successfully authenticated

Received first command on ingress connection since session start or auth handshake

Connection accepted

client metadata

WiredTiger message

And then I tried to fetch the document again and got this:

---DOC null


2 years ago

Looks like you are trying to use more memory than your trial plan allows for, upgrade to the Hobby plan and redeploy.


preston206
HOBBYOP

2 years ago

hi, I upgraded and re-deployed Node and MongoDB. I'm still getting null.


preston206
HOBBYOP

2 years ago

bump


2 years ago

Sorry I don't know what null means in this context, please more more information / error logs.


preston206
HOBBYOP

2 years ago

the context is in the first post and I've posted the only errors occurring. Is this the only support that Railway offers? Do you have anybody helping you out in this forum?


preston206
HOBBYOP

2 years ago

Solved. I figured it out. I had a missing variable in my Node service.


Welcome!

Sign in to your Railway account to join the conversation.

Loading...