Joséph Flames Joséph Flames - 8 days ago 9
Node.js Question

Pg not working heroku

I've litterally tried every possible solution on the Internet and still no luck with implmeeting the database

EDIT: for further code review here's the github.com link
https://github.com/flaminggenius/lit-fortress/blob/lit-fortress/src/server/server.js

I got this from the heroku docs but it doesn't do anything

var con = "postgres://username:pass@host:port/database"; //yes I did actually get the credentials from heroku but I'm not putting them here
pg.defaults.ssl = true;
pg.connect(con, function(err, client) {
if (err) {
console.log (err);
console.log ("POSTGRES FAILED TO CONNECT");
}
console.log('Connected to postgres! Getting schemas...');

client
.query('SELECT table_schema,table_name FROM information_schema.tables;')
.on('row', function(row) {
console.log (JSON.stringify(row));
});
});


This doesn't crash the app but if I change the
client
to

client.query ("INSERT INTO users SET name='joey'");


The app then crashes

Can anyone help me here are some questions I need answered

1.How do I properly establish a connection
2.How do I end connection (pg.end?)
3.How do I insert data?
4.How do I retrieve data

Before anyone says "what have you tried?" I have lithe rally done every Web search I could possibly think of for nodeJS,heroku, and pg and every link the code ether crashes the app or doesn't work....

Here is log

Dec 01 11:18:58 masswarfare-dev app/web.1: syscall: 'connect',
Dec 01 11:18:58 masswarfare-dev app/web.1: address: '10.179.216.254',
Dec 01 11:18:58 masswarfare-dev app/web.1: port: 3306,
Dec 01 11:18:58 masswarfare-dev app/web.1: fatal: true }
Dec 01 11:18:58 masswarfare-dev app/web.1: Connected to postgres! Getting schemas...
Dec 01 11:18:58 masswarfare-dev app/web.1: events.js:160
Dec 01 11:18:58 masswarfare-dev app/web.1: throw er; // Unhandled 'error' event
Dec 01 11:18:58 masswarfare-dev app/web.1: ^
Dec 01 11:18:58 masswarfare-dev app/web.1: error: syntax error at or near "SET"
Dec 01 11:18:58 masswarfare-dev app/web.1: at Connection.parseE (/app/node_modules/pg/lib/connection.js:554:11)
Dec 01 11:18:58 masswarfare-dev app/web.1: at Connection.parseMessage (/app/node_modules/pg/lib/connection.js:381:17)
Dec 01 11:18:59 masswarfare-dev app/web.1: at TLSSocket.<anonymous> (/app/node_modules/pg/lib/connection.js:117:22)
Dec 01 11:18:59 masswarfare-dev app/web.1: at emitOne (events.js:96:13)
Dec 01 11:18:59 masswarfare-dev app/web.1: at TLSSocket.emit (events.js:188:7)
Dec 01 11:18:59 masswarfare-dev app/web.1: at readableAddChunk (_stream_readable.js:176:18)
Dec 01 11:18:59 masswarfare-dev app/web.1: at TLSSocket.Readable.push (_stream_readable.js:134:10)
Dec 01 11:18:59 masswarfare-dev app/web.1: at TLSWrap.onread (net.js:548:20)
Dec 01 11:18:59 masswarfare-dev app/web.1: [19:18:58] [nodemon] app crashed - waiting for file changes before starting... `


Package.json

{
"name": "masswarfare",
"version": "1.0.0",
"description": "A simple Agar.io clone",
"main": "server/server.js",
"scripts": {
"build": "gulp build",
"start": "gulp run",
"watch": "gulp watch",
"test": "gulp test"
},
"repository": {
"type": "git",
"url": "https://github.com/huytd/agar.io-clone.git"
},
"author": "Huy Tran",
"license": "MIT",
"contributors": [
"Igor Antun <igorantunc@hotmail.com> (https://github.com/IgorAntun)",
"Bjarne Oeverli <bjarne.oeverli@gmail.com> (https://github.com/bjarneo)",
"Chris Morgan <chris.j.r.morgan@gmail.com> (https://github.com/drpotato)",
"PET Computação UFPR <pet@inf.ufpr.br> (http://pet.inf.ufpr.br)",
"Saren Currie <saren@sarencurrie.com> (https://github.com/SarenCurrie)",
"Nguyen Huu Thanh <giongto35@yahoo.com> (https://github.com/giongto35)",
"Ariamiro <dossantosgusmaosavio@gmail.com> (https://github.com/Ariamiro)",
"VILLERS Mickaël <mickael.villers@epitech.eu> (https://github.com/villers)",
"Dan Prince <dan@astrealdynamics.co.uk> (https://github.com/danprince)",
"Kostas Bariotis <konmpar@gmail.com> (https://github.com/kbariotis)",
"Jerry Xu <jerryxu23@gmail.com> (https://github.com/abalabahaha)",
"Damian Dlugosz <bigfootdd@gmail.com> (https://github.com/bigfoot90)",
"Juha Tauriainen <juha@bin.fi> (https://github.com/JuhQ)",
"Madara Uchiha <me@madara.ninja> (https://github.com/MadaraUchiha)",
"Keith Groves <Keithalgroves@gmail.com> (https://github.com/buskcoin)",
"wb9688 <wbertrums@gmail.com> (https://github.com/wb9688)"
],
"dependencies": {
"babel": "^6.5.2",
"babel-core": "^6.9.1",
"babel-loader": "^6.2.4",
"babel-preset-es2015": "^6.14.0",
"chai": "^3.5.0",
"express": "^4.13.4",
"gulp": "^3.9.1",
"gulp-babel": "^6.1.2",
"gulp-jshint": "^2.0.1",
"gulp-mocha": "^2.2.0",
"gulp-nodemon": "^2.0.7",
"gulp-todo": "^4.1.0",
"gulp-uglify": "^1.5.3",
"gulp-util": "^3.0.7",
"jshint": "^2.9.2",
"mocha": "^2.5.3",
"node-libs-browser": "^1.0.0",
"nodemon": "^1.9.2",
"sat": "^0.5.0",
"simple-quadtree": "^0.1.3",
"socket.io": "^1.4.6",
"socket.io-client": "^1.4.6",
"sync-request": "^3.0.1",
"webpack": "^1.13.1",
"webpack-stream": "^3.2.0",
"mysql": "^2.5.5",
"pg": "^6.1.0"
}
}

Answer

Well, according to your logs:

Dec 01 11:18:58 masswarfare-dev app/web.1: error: syntax error at or near "SET"

Doing a quick scan of the Postgres docs tells me that your query of

INSERT INTO users SET name='joey'

Is, in fact, invalid syntax. SET is a command that is used with UPDATE queries, not INSERT queries. A correct INSERT query would look like:

INSERT INTO users (name) VALUES ('joey');

As a side note, you really should try and run your code locally, even if it's just your test suite as it's often easier to find this kind of thing early.

Further, if your server.js is 1000 lines, I would argue that you're doing it wrong. The code will be much more maintainable if you break things out into smaller files that you can compose. I rarely have to scroll a file, and never more than two pages.