通过 Heroku Connect 上的 salesforce trailhead 工作。在更改和重新部署应用程序步骤之前,一切都已正常运行。停止应用程序并修改 server.js 文件后,我无法连接/更新数据库记录。恢复了 server.js 文件,同样的问题。我在 Windows 7 上,npm --version = 4.6.1,节点 --version = v8.11.1,git --version = 2.14.2.windows.2。知道可能导致以下错误的原因:
{
Error: connect ECONNREFUSED 127.0.0.1:5432
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1198:14)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 5432
}
谢谢!
以下是设置/启动应用程序的命令:
SET DATABASE_URL=`heroku config:get DATABASE_URL`
SET PGSSLMODE=require
npm start
这是 server.js 文件的内容:
var express = require('express');
var bodyParser = require('body-parser');
var pg = require('pg');
var app = express();
app.set('port', process.env.PORT || 5000);
app.use(express.static('public'));
app.use(bodyParser.json());
app.post('/update', function(req, res) {
pg.connect(process.env.DATABASE_URL, function (err, conn, done) {
// watch for any connect issues
if (err) console.log(err);
conn.query(
'UPDATE salesforce.Contact SET Phone = $1, MobilePhone = $1 WHERE LOWER(FirstName) = LOWER($2) AND LOWER(LastName) = LOWER($3) AND
LOWER(Email) = LOWER($4)',
[req.body.phone.trim(), req.body.firstName.trim(),
req.body.lastName.trim(), req.body.email.trim()],
function(err, result) {
if (err != null || result.rowCount == 0) {
conn.query('INSERT INTO salesforce.Contact (Phone,
MobilePhone, FirstName, LastName, Email) VALUES ($1, $2, $3, $4, $5)',
[req.body.phone.trim(), req.body.phone.trim(),
req.body.firstName.trim(), req.body.lastName.trim(),
req.body.email.trim()],
function(err, result) {
done();
if (err) {
res.status(400).json({error: err.message});
}
else {
// this will still cause jquery to display 'Record
updated!'
// eventhough it was inserted
res.json(result);
}
});
}
else {
done();
res.json(result);
}
}
);
});
});
app.listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'));
});