Any way to trigger database connection on page load?

When I try to log in to a website I built, Render throws the following error:

  if (results.length > 0) { 
TypeError: Cannot read properties of undefined (reading 'length')

It then connects to the database and the login works perfectly, provided I try to log in again about 15-20 seconds later. But in a real-world enviroment, a user wouldn’t know to wait 20 seconds and then try again!

I’ve determined that the reason for this is that ‘results’ is undefined until such time as a connection with the database has been established. The login request ( is calling this code before a connection with the DB has been established.

How can I get around this?

  • Is there a way of getting the web app to establish a connection with the database the moment a user lands on the home page, so that when they come to log in, the connection is already live?

  • Is there a way to have the connection to the database be permanent and never drop?

  • Is there a way to trigger the database connection and get the login code to wait until the connection is established before executing?

(I’m very new to this, so please make your replies as non-technical as possible :slight_smile: )

Hey Nigel,

Welcome to Render and your journey building internet applications.

What you’re asking isn’t specific to Render at all, but I’ll provide some details to guide you here.

Fortunately, most of what you’re doing is just boiler-plate, frameworks, libraries exist for all of the bits you need here, that handling making connections, terminating them when not needed etc

As for the specific error you are getting here. It would seem you’re trying to call the length method on results when results does not yet exist, you might need to test for the presence of results first, pseudo code:

 *if (results && results.length > 0) { *

Equally, you might be using an asynchronous language like Node, so any interactions with the database may need to be fulfilled via a promise before you can use the results - something I myself have always stumbled on when I try and write JS.

Largely, all of this is entirely outside of Render support as we’re here to Support the platform and not your code but hopefully this has given you some pointers,


Thanks for the reply. It’s useful to know that this is an issue with the code as opposed to Render. The problem with testing my code in a local environment is that the connection to the server is already established and so the error I’m getting on Render never crops up!

I had already attempted the ‘results && results.length’ change that you have kindly suggested. Unfortunately, since during the login attempt ‘results’ doesn’t yet exist, this just causes the code to jump to the ‘else’ clause (with the error message) and doesn’t trigger the connection to the database.

Yes, I’m using Node, so it sounds like the asynchronous nature of it is giving me the issues. I’ll have to dig a bit deeper to see if I can figure out how to solve it.

So, I edited the code to make the login function asynchronous, using async / await, but then nothing happens at all. I assume this is because there is nothing causing the app to connect to the database, so the function is waiting but getting no response.

It seems the problem is arising because nothing is triggering a connection to the database, other than the code that is giving rise to the problem.