0

I have cloned the following repo from github:

https://github.com/FACed-Off/learn-node-postgres

and am current working on the following commit hash in the master branch:

3785d42e8e8a954a363ee108d5263bb51780a0ea

However I am unable to progress at the point where I run the server, as the init.sql file does not seem to be recognised. VScode claims there is a syntax error despite the code being correct, however upon running this code on another machine, the expected output occurs which is:

"...a big object logged in your terminal. This is the entire result of our database query. The bit we're actually interested in is the "rows" property. This is an array of each row in the table we're selecting from. Each row is represented as an object, with a key/value entry for each column."

The output I get on this current machine upon running npm run dev is the "hello world" text at the localhost:3000 endpoint, however I do not get any sort of output from the code in the home function for the db.query method:

  db.query("SELECT * FROM users").then((result) => {
    console.log(result);
  });

I have verified that postgres is installed by running which psql, to which I get the output /usr/local/bin/psql

I have verified that the local database is setup by running

pgcli
\connect learn_node_postgres
SELECT * FROM users

and receiving the expected output of a table representing all the entries in the users table in the terminal

I have verified that the required npm modules of dotenv and pg have been installed by checking the dev dependencies and seeing

"dependencies": {
    "dotenv": "^8.2.0",
    "nodemon": "^2.0.2",
    "pg": "^7.18.2"
  },
Meeran
  • 91
  • 5

1 Answers1

0

After some research, and receiving help from a 3rd party, I came across the solution to my own issues here.

the init.sql file does not seem to be recognised. VScode claims there is a syntax error despite the code being correct

There is an extension in vscode that is suggested when writing sql code called mssql, this does not lint as intented, so disabling this solved the issue.

However, this did not solve the fact that my code was not outputting my database correctly, the solution to this was found here.

The node version I had, v14.5.0 was not compatible with the pg package version v7.18.2. I had to change the package.json file to allow it to upgrade to the latest version of pg when initialising the pg package with npm i. The caret(^) symbol originally used in the package.json file with this dev dependency meant that reinstalling the pg package did not get the v8 versions of pg which would be compatible with the v14.5.0 version of node. More details can be found here

After entirely removing the line "pg": "^7.18.2" from my package.json and reinitiallising pg, this got the latest version and solved my issue.

Here is a post summarising the issue and solution

Meeran
  • 91
  • 5