当 NodeJS 被要求与 PostgreSQL 对话时应用程序崩溃
App Crashes When NodeJS is Asked to Talk to PostgreSQL
我在网络开发方面有一点经验,但我迄今为止建立的网站都有嵌入式数据库。这是我第一次尝试使用 非嵌入式 数据库构建网站。
我使用 NodeJS 和 Express 框架构建了一个骨架站点。它由 Heroku 托管。在我尝试添加数据库之前,该应用程序似乎运行良好,正如预期的那样。
但是,现在我已将 scraper.js
文件添加到应用程序,我得到的只是应用程序已崩溃的通知。这个文件的内容是:
const Finaliser = require("./finaliser.js");
const { Client } = require("pg");
const client = new Client({
connectionString: process.env.DATABASE_URL,
ssl: true,
});
class Scraper
{
constructor()
{
this.finaliser = new Finaliser();
}
fetchAsIs(req, res)
{
var data, columns, rows;
var tableName = req.params.id;
var that = this;
var queryString = "SELECT * FROM "+tableName+";";
client.connect();
client.query(queryString, (err, extract) => {
if(err) throw err;
client.end();
console.log(extract);
that.finaliser.protoRender(req, res, "asis",
{ title: tableName });
});
}
};
module.exports = Scraper;
额外的细节:
- 运行
heroku logs --tail
给出:
2019-10-23T15:35:50.281517+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=harvesthub.herokuapp.com request_id=caadba91-9b2d-4525-9c9c-19c34733073d fwd="194.33.13.237" dyno= connect= service= status=503 bytes= protocol=https
2019-10-23T15:35:50.588155+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=harvesthub.herokuapp.com request_id=28b7948f-ff59-44f0-867a-f3ce7ede86cc fwd="194.33.13.237" dyno= connect= service= status=503 bytes= protocol=https
- 我认为数据库设置正确。 运行
heroku addons --app harvesthub
给出:
Add-on Plan Price State
──────────────────────────────────────────────── ───────── ───── ───────
heroku-postgresql (postgresql-crystalline-06305) hobby-dev free created
└─ as DATABASE
The table above shows add-ons and the attachments to the current app (harvesthub) or other apps.
应用程序源代码的存储库是 https://github.com/tomhosker/harvesthub。
我知道"SELECT * FROM "+tableName+";"
是悲伤的良方。待会再改。
抱歉问题这么长!
问题是我未能正确安装 pg
和 dotenv
软件包。 (我曾 尝试 安装它们,但由于某种原因没有成功。)
任何发现自己和我处境相同的人:仔细检查 pg
和 dotenv
是否在您的 package.json
文件中。如果它们丢失,运行 npm install --save pg dotenv
安装它们。
衷心感谢 Justin Moser。这个答案只是他评论的充实。
我在网络开发方面有一点经验,但我迄今为止建立的网站都有嵌入式数据库。这是我第一次尝试使用 非嵌入式 数据库构建网站。
我使用 NodeJS 和 Express 框架构建了一个骨架站点。它由 Heroku 托管。在我尝试添加数据库之前,该应用程序似乎运行良好,正如预期的那样。
但是,现在我已将 scraper.js
文件添加到应用程序,我得到的只是应用程序已崩溃的通知。这个文件的内容是:
const Finaliser = require("./finaliser.js");
const { Client } = require("pg");
const client = new Client({
connectionString: process.env.DATABASE_URL,
ssl: true,
});
class Scraper
{
constructor()
{
this.finaliser = new Finaliser();
}
fetchAsIs(req, res)
{
var data, columns, rows;
var tableName = req.params.id;
var that = this;
var queryString = "SELECT * FROM "+tableName+";";
client.connect();
client.query(queryString, (err, extract) => {
if(err) throw err;
client.end();
console.log(extract);
that.finaliser.protoRender(req, res, "asis",
{ title: tableName });
});
}
};
module.exports = Scraper;
额外的细节:
- 运行
heroku logs --tail
给出:
2019-10-23T15:35:50.281517+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=harvesthub.herokuapp.com request_id=caadba91-9b2d-4525-9c9c-19c34733073d fwd="194.33.13.237" dyno= connect= service= status=503 bytes= protocol=https
2019-10-23T15:35:50.588155+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=harvesthub.herokuapp.com request_id=28b7948f-ff59-44f0-867a-f3ce7ede86cc fwd="194.33.13.237" dyno= connect= service= status=503 bytes= protocol=https
- 我认为数据库设置正确。 运行
heroku addons --app harvesthub
给出:
Add-on Plan Price State
──────────────────────────────────────────────── ───────── ───── ───────
heroku-postgresql (postgresql-crystalline-06305) hobby-dev free created
└─ as DATABASE
The table above shows add-ons and the attachments to the current app (harvesthub) or other apps.
应用程序源代码的存储库是 https://github.com/tomhosker/harvesthub。
我知道
"SELECT * FROM "+tableName+";"
是悲伤的良方。待会再改。抱歉问题这么长!
问题是我未能正确安装 pg
和 dotenv
软件包。 (我曾 尝试 安装它们,但由于某种原因没有成功。)
任何发现自己和我处境相同的人:仔细检查 pg
和 dotenv
是否在您的 package.json
文件中。如果它们丢失,运行 npm install --save pg dotenv
安装它们。
衷心感谢 Justin Moser。这个答案只是他评论的充实。