0

So i have this small Node JS app where i have the following script, which i invoke in my HTML index page, in order to connect to a Cloud SQL database in GCP and perform a specific query so i can pass the values to a dropdown later:

try {
  pool = new Pool({
    user: "postgres",
    host: "/cloudsql/sfmcsms-d-970229:europe-west1:dsi-sfmc-sms-database",
    database: "postgres",
    password: "dsi-sfmc-sms-database",
    port: "5432",
  });

  console.log("Connection successfull!");

  pool.query("select * from ConfigParameter;", (error, results) => {
    if (error) {
      console.log(error);
    }
    qResult = results;
    console.log(qResult);
    //insert logic to populate dropdowns
  });
} catch (err) {
  console.log("Failed to start pool", err);
}

I'm still working on the logic to populate the dropdowns but for now, i'm focusing on establishing a successful connection first before i get to that. However, everytime i run the script, i seem to get this particular error:

ReferenceError: Pool is not defined

I've been looking around for some possible answers but no luck.

B. Palma
  • 1
  • 1

1 Answers1

0

Before using Pool you have import first like this

const { Pool } = require('pg')

And obviously node-postgres should be installed

npm i pg
  • I did all that but i get this error when i open the console: Uncaught Error: Module name "pg" has not been loaded yet for context: _. Use require([]) – B. Palma Jul 13 '22 at 12:42
  • Please see this answer https://stackoverflow.com/questions/17446844/dynamic-require-in-requirejs-getting-module-name-has-not-been-loaded-yet-for-c – Diwas Acharya Jul 13 '22 at 13:17
  • So i modified my coded as suggested in that link but i keep getting these errors: `GET http://localhost:3000/js/pg.js net::ERR_ABORTED 404 (Not Found) Uncaught Error: Script error for "pg", needed by: connectDB` seems like it's looking for a "pg.js" file but i installed the package with "npm i pg". Do i need to manually add the "pg.js" source code file to my project? – – B. Palma Jul 13 '22 at 15:16