主键时出现 UnhandledPromiseRejectionWarning"/>
在 Node pg (Postgresql) 中返回主键时出现 UnhandledPromiseRejectionWarning
我有这段代码可以查询我的 postgres db
getLeagueId = async (league_name) => {
// Return the league id from a given league
const client = this.connectToDatabase();
await client.connect();
try {
const query = await client.query(`SELECT * FROM leagues
WHERE name = '${league_name}';`);
await client.end();
return query.rows[0];
} catch (err) {
console.log(`An error occurred when fetching league ID: ${err}`);
}
};
我得到回报:
{"league_id":1,"name":"Premier League","type":"League","country":"England","logo":".png","flag":".svg"}
当我把return改成
return query.rows[0].name;
// I get the name Premier League
一旦我把回报改成
return query.rows[0].league_id;
我得到以下错误
express deprecated res.send(status): Use res.sendStatus(status) instead app.js:52:11
(node:274778) UnhandledPromiseRejectionWarning: RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: 1
at ServerResponse.writeHead (_http_server.js:248:11)
at ServerResponse._implicitHeader (_http_server.js:239:8)
at write_ (_http_outgoing.js:654:9)
at ServerResponse.end (_http_outgoing.js:766:5)
at ServerResponse.send (/home/stoffel/Documents/dailySports/node_modules/express/lib/response.js:232:10)
at ServerResponse.json (/home/stoffel/Documents/dailySports/node_modules/express/lib/response.js:278:15)
at ServerResponse.send (/home/stoffel/Documents/dailySports/node_modules/express/lib/response.js:162:21)
at /home/stoffel/Documents/dailySports/app.js:55:11
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:274778) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see .html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:274778) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
每次我尝试访问主键时都会出错。我试过 postgres.js 和 pg 都给了我这个错误
回答如下:Express
res.send()
只发送对象、字符串或数组类型的响应。您不能发送整数类型的响应。您的 league_id
值类型是整数。
访问此链接了解更多信息:https://expressjs/en/5x/api.html#res.send
更多推荐
在 Node pg (Postgresql) 中返回主键时出现 UnhandledPromiseRejectionWarning
发布评论