在 Node pg (Postgresql) 中返回主键时出现 UnhandledPromiseRejectionWarning

编程入门 行业动态 更新时间:2024-10-08 19:50:05

在 Node pg (Postgresql) 中返回<a href=https://www.elefans.com/category/jswz/34/1771236.html style=主键时出现 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

本文发布于:2024-05-13 16:12:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1760022.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:主键   pg   Node   UnhandledPromiseRejectionWarning   Postgresql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!