【问题标题】:knex.js - simpler way to update dataknex.js - 更新数据的更简单方法
【发布时间】:2017-08-14 18:36:35
【问题描述】:

我想更新一个名为 client 的表。以下代码适用于有效的 id,但对于无效的,它只是挂起而不调用 catch 函数。

knex('client').where('id',id).update({
        name:req.body.name
        }).then(function(numberOfUpdatedRows) {
            if(numberOfUpdatedRows) {
                res.json(success);
                return;
            }
        }).catch(function(err){
            res.status('500');
            res.json(err);
            return;         
        }); 
});

我解决了这样的问题:

knex('client').where('id',id).select('id').then(function(id){
        if(id) {
            //update 
        }else {
            //failed
        }
    }).catch(function(err){
        console.log("select id do not exist");
        res.send("do not exist");
    });

对于这种情况,对于无效的 id,knex 不会调用 catch 函数,而是挂断返回一个空的 id 值,我用它来检查错误。
我是初学者,我确信有更好的方法来做到这一点。
有人可以提出更好的方法吗?
此外,我在knex documentation 中找不到太多关于 knex 如何处理此类错误情况的信息。那么我在哪里可以找到这些信息,以便我将来自己解决它们。谢谢。

【问题讨论】:

  • 首先验证您的数据。
  • 查看我的解决方案。 @FazalRasel 不需要验证。

标签: mysql node.js knex.js


【解决方案1】:
knex
  .select('id')
  .from('client')
  .where('id', id)
  .then(([row]) => {
    if (!row) {
      console.log("select id do not exist")
      return res.send("do not exist")
    }
    return knex('client')
      .update('name', req.body.name)
      .where('id', row.id)
  });

如果我已经知道该行可能不存在,我会将其视为应由代码处理的逻辑错误。

【讨论】:

    【解决方案2】:

    像这样使用 knex

    const {id,name} = req.body;
        const subQuery = knex('client').select('id').where({id})
        subQuery.then(response=>{
        if(response.length>0){
            subQuery.update({name})
            .then(resp=>{
                res.json('update done')
            })
            .catch(err=>{res.json(err)})
        }
        else{
            res.json('update failed')
         }
    })
    .catch(err=>{res.json(err)})
    

    【讨论】:

      【解决方案3】:

      您的代码执行没有问题,这就是不执行catch 方法的原因。一个在更新时捕获未发现错误的巧妙方法是这样的

      const {name} = req.body;
      knex("client")
        .update({name})
        .where({id})
      .then(rows => {
        // the argument here as you stated
        // describes the number of rows updated
        // therefore if no row found no row will be updated
        if (!rows){
          return res.status(404).json({success:false});
        }
        return res.json({success:true});
      })
      .catch( e => res.status(500).json(e)); 
      

      或者如果你喜欢一行

      const {name} = req.body;
      knex("client")
        .update({name})
        .where({id})
        .then(u => res.status(!!u?200:404).json({success:!!u}))
        .catch(e => res.status(500).json(e));
      

      【讨论】:

        【解决方案4】:

        一个很好的例子就是这个

        db("table")
        .update({deleted_at: "now()"})
        .where("column", "value");
        

        如果您想了解更多信息,请查看:http://knexjs.org/#Builder-update

        【讨论】:

          猜你喜欢
          • 2014-03-12
          • 2011-06-14
          • 2015-12-03
          • 2019-04-24
          • 1970-01-01
          • 2020-09-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多