Good day to any kindred spirit or knowledgeable fella.
I'm trying to implement an H2 database connection (the database is running on server mode so I can make multiple connections via TCP/IP) in a web server and i found myself in the same state every time, the function is called and it returns the promise in a pending status which i leave with an await that ends up just going and going, without ever getting the answer from the query.
The stack i'm using is
- node (12.11.0, don't blame me, it was the client's requirement)
- expressjs, since the client only needs 3 endpoints
- H2 database already existing on my client's server for authentication
- SQL Server, which has the core data of the business and I'll be using after getting the auth done.
Call of the function
userModel.getUsers();
The function getUsers
try {
const connection = await h2.getInstance().getConnection();
console.log('Connection established');
return connection.query('SELECT * FROM USERS');
} catch (error) {
console.error(error);
throw error;
}
H2 connection class as you will see I'm using the pg library for handling the connection since it can be done via TCP/IP, but have also tried using jdbc with the same result.
class H2Connection {
// Use a private static field to store the instance
static #instance = null;
// Use a private constructor to prevent direct instantiation
constructor() {
this.connection = new pg.Client(h2Conf);
this.connection.connect();
}
static getInstance() {
// If the instance does not exist, create a new one
if (H2Connection.#instance == null) {
H2Connection.#instance = new H2Connection();
}
// Return the instance
return H2Connection.#instance;
}
// Use a method to get the connection
getConnection() {
return this.connection;
}
}
Hope anyone can help me with this one.
Tried:
- Changed the handling of callbacks to promises and vice versa.
- Tried to use await on the call of the function to receive the promise resolved but it never is.
- Debugged the code to check for any errors that might not be catching, but it ends up going back and forth in the waiting for request internal process of the web server.
- Tried to resolve the promise and return the values needed in a local variable inside the function but outside the scope of the promise/callback.
Expected:
- Logging of any kind after the call of the service which i got when not using await on the call of the function but it always assigned undefined values to the variable that later i checked in the logic.
- Tried to get logging from the usage of then-catch, but also never got any inside or outside the function.
- The result from the query in an array or json format.