1

我正在尝试使用 Knex.js 更新表中的多个列

我曾尝试查看 Knex 文档以及在线中的建议,但到目前为止还没有找到任何可行的解决方案。

我的编码尝试:

const userUpdateHandler = (req,res,database)=>{
  const { id } = req.params;
  const { name, age } = req.body.formInput
  database('users')
  .where({ id })
  .update({ name }).update({ age})
  .then(resp => {
    if (resp) {
      res.json("success")
    } else {
      res.status(400).json('Not found')
    }
  })
  .catch(err => res.status(400).json('error updating user'))

}

以上是我出于绝望而尝试过的内容,但我想做的是一次更新多个列。你能告诉我最好的方法吗?

4

1 回答 1

0

这应该工作https://runkit.com/embed/6ulv4hy93fcj

knex('table').update({foo: 1}).update({bar: 2}).toSQL()

// generates SQL: update "table" set "foo" = ?, "bar" = ?

所以应该:

database('users')
  .where({ id })
  .update({ name, age })

检查您生成的查询是否是您所期望的...

于 2019-09-11T19:50:30.747 回答