【发布时间】:2020-05-12 04:54:54
【问题描述】:
所以我有来自 GET URL 的信息,这些信息需要传递到 JSON 中,然后在 PostgreSQL DBMS 中保存(与增加的 ID 聚合以确保正确)。我写了以下代码,似乎没有保存任何错误:
// Pg initialization
const { Client } = require('pg')
client = new Client({
host: 'localhost',
user: 'postgres',
password: 'passwordhere',
database: 'dbnamehere',
});
const createTableText = `
CREATE EXTENSION IF NOT EXISTS "pgcrypto";
CREATE TEMP TABLE IF NOT EXISTS cases (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
data JSONB
);
`
app.get('/test', async function (req, res) {
data = req.query.data;
console.log(data);
res.status(200).send(data);
// create our temp table
await client.query(createTableText)
//const newUser = { email: 'test@test.com' }
// create a new case
await client.query('INSERT INTO cases(data) VALUES($1)', [data])
const { rows } = await client.query('SELECT * FROM cases')
console.log(rows)
res.end();
});
我的 package.json 依赖项:
"dependencies": {
"express": "^4.17.1",
"mongoose": "^5.9.9",
"pg": "^8.0.3"
},
"devDependencies": {}
更新
我在文件末尾有这个错误处理代码:
// Prints out more detailed errors
if(process.env.NODE_ENV !== 'production') {
process.once('uncaughtException', function(err) {
console.error('FATAL: Uncaught exception.');
console.error(err.stack||err);
setTimeout(function(){
process.exit(1);
}, 100);
});
}
我还尝试安装npm install express-promise-router 并添加以下代码,但没有打印错误:
var router = require('express-promise-router')();
router.use('/test', function (req, res) {
return Promise.reject();
})
更新2 这段代码没有关闭它会打印出 JSONB,而不是如何保存它?:
const connectionString=urlhere;
const pool = new Pool({
connectionString: connectionString,
})
const client = new Client({
connectionString: connectionString,
})
client.connect()
更新3:
我删除了异步代码并使其同步。我现在收到以下错误消息:
(node:10860) UnhandledPromiseRejectionWarning: Error: Connection terminated
at Connection.<anonymous> (/path/here/node_mo
dules/pg/lib/client.js:275:34)
at Object.onceWrapper (events.js:299:28)
at Connection.emit (events.js:215:7)
at Socket.<anonymous> (/path/here/node_module
s/pg/lib/connection.js:73:10)
at Socket.emit (events.js:210:5)
at TCP.<anonymous> (net.js:659:12)
(node:10860) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This
error originated either by throwing inside of an async function without a catch
block, or by rejecting a promise which was not handled with .catch(). (rejectio
n id: 1)
(node:10860) [DEP0018] DeprecationWarning: Unhandled promise rejections are depr
ecated. In the future, promise rejections that are not handled will terminate th
e Node.js process with a non-zero exit code.
(node:10860) UnhandledPromiseRejectionWarning: Error: Connection terminated
at Connection.<anonymous> (/path/here/client.js:275:34)
at Object.onceWrapper (events.js:299:28)
at Connection.emit (events.js:215:7)
at Socket.<anonymous> (/path/here/node_module
s/pg/lib/connection.js:73:10)
at Socket.emit (events.js:210:5)
at TCP.<anonymous> (net.js:659:12)
(node:10860) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This
error originated either by throwing inside of an async function without a catch
block, or by rejecting a promise which was not handled with .catch(). (rejectio
n id: 2)
(node:10860) UnhandledPromiseRejectionWarning: Error: Connection terminated
at Connection.<anonymous> (/path/here/node_mo
dules/pg/lib/client.js:275:34)
at Object.onceWrapper (events.js:299:28)
at Connection.emit (events.js:215:7)
at Socket.<anonymous> (/path/here/node_module
s/pg/lib/connection.js:73:10)
at Socket.emit (events.js:210:5)
at TCP.<anonymous> (net.js:659:12)
(node:10860) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This
error originated either by throwing inside of an async function without a catch
block, or by rejecting a promise which was not handled with .catch(). (rejectio
n id: 3)
【问题讨论】:
-
肯定有错误。您是否使用像 express-promise-router 这样的承诺支持路由器?你需要它来保证正确地产生错误。 (如果这是问题所在,您的控制台上还应该有一个未处理的 Promise 拒绝警告。)
-
@Ry- 所以我需要先用
npm install express-promise-router安装它?为了清楚起见,我添加了 package.json 依赖项。 -
我安装了,仍然没有错误:/
-
你不能停止安装它;你也必须使用它。阅读软件包的文档 (README)。
-
@Ry- 它似乎没有帮助:/ 我尝试添加更新问题中提到的代码
标签: javascript node.js postgresql express node-postgres