【问题标题】:Node.js express and postgress sql pool.queryNode.js express 和 postgresql pool.query
【发布时间】:2021-04-15 17:05:56
【问题描述】:

我在将 pool.query 的结果添加到变量时遇到问题(使用 pool.query 我只选择一个值,id 值,我需要插入到其他表中)。到目前为止,我有这个,但 console.log 给了我

承诺{ }

router.get("/restaurants/add",checkNotAuthenticated, async (req, res) => {

    var  x = pool.query(`select delivery.id from delivery order by id desc limit 1`);
    console.log(x);


    /*pool.query(
        `INSERT INTO new_table (newId)
                    VALUES ($1)`,
        [x]);*/
    res.redirect("/restaurants/dashboard")
});

任何帮助都会很有价值。

【问题讨论】:

  • 看起来你缺少 await 所以承诺没有解决。

标签: sql node.js postgresql express web-applications


【解决方案1】:

您可以直接使用insert into .. select ..,如下:

Insert into new_table (column_name)
select delivery.id from delivery order by id desc limit 1

【讨论】:

  • 是的,这是一个很好的方法,但现在我遇到了问题,因为我需要在同一张表中再添加一列,值为 req.user.id
猜你喜欢
  • 2019-05-25
  • 1970-01-01
  • 2021-12-06
  • 1970-01-01
  • 2019-05-13
  • 2014-03-20
  • 1970-01-01
  • 2013-07-25
  • 2017-06-25
相关资源
最近更新 更多