我已尝试在互联网上尝试所有可能的解决方案,但仍无法推动数据库的发布
编辑:有关进一步的代码审查,请访问github.com链接 https://github.com/flaminggenius/lit-fortress/blob/lit-fortress/src/server/server.js
我从heroku文档中得到了这个,但它没有做任何事情
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));
});
});
如果我将client
更改为
client.query ("INSERT INTO users SET name='joey'");
该应用程序然后崩溃
任何人都可以帮助我这里有一些我需要回答的问题
1.如何正确建立连接 2.如何结束连接(pg.end?) 3.如何插入数据? 4.如何检索数据
在有人说'#34;你尝试了什么?"对于nodeJS,heroku和pg以及代码以太网崩溃应用程序或无法工作的每个链接,我都进行了每次网络搜索的轻松集会....
这是日志
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"
}
}
答案 0 :(得分:0)
好吧,根据您的日志:
Dec 01 11:18:58 masswarfare-dev app/web.1: error: syntax error at or near "SET"
快速扫描Postgres文档告诉我您的查询
INSERT INTO users SET name='joey'
实际上是无效的语法。 SET是一个与UPDATE查询一起使用的命令,而不是INSERT查询。正确的INSERT查询看起来像:
INSERT INTO users (name) VALUES ('joey');
作为旁注,你真的应该尝试在本地运行你的代码,即使它只是你的测试套件,因为它通常更容易提前找到这种东西。
此外,如果您的server.js
是1000行,我会认为您做错了。如果您将内容分解为可以编写的较小文件,则代码将 更易于维护。我很少滚动文件,也不会超过两页。