【问题标题】:Promise handling - update db entry if exists承诺处理 - 如果存在则更新数据库条目
【发布时间】:2018-01-20 16:04:11
【问题描述】:

我在 Promise 上遇到了新的挑战。

目标:仅当 P_KEY 存在时才更新 DB 条目。

当前 db 通过模块公开,模块有 db 的 get 和 put 方法。两者都返回 Promise。

方法:

  1. API 调用节点 js 上带有 ID 和值集 (json) 的更新方法处理程序
  2. 在对 db 模块 get 方法的 post 方法调用的处理程序中,检查承诺成功的值是否为空,如果是,则返回 false,否则返回 true。
  3. 如果为真;数据存在对 db 模块的put 方法的调用。

但不知何故,它总是返回错误的数据。即使已经通过 db api 进行了 db 条目。

/** Function to check if p_key exist*/
function checkIfPKExists(idVal){
  pkdb.get(idVal).then(function(value){
    if(value){
      return true;
    } else {
      return false;
    }
  },
  function(err){
    console.log(err);
    return false;
  })
}

/** UPDATE METHOD **/
var ch = checkIfPKExists("p_k"+req.body.id);
if(!ch){
  res.send("pk does not exist oo " + req.body.id);
} else {
  var pk_promise = pkdb.put("p_k"+req.body.id, req.body.pk);
  pk_promise.then(
    function(){
      res.send(JSON.stringify(req.body.pk) + "Updated Successfully");
    },
    function(err){
      res.send("Error occurred : " + err);
    }
  )
}

我的理解是ch 的值是从checkPK 函数设置的,因为这是一个承诺,所以它会继续处理if 循环,默认情况下代表true 并且无论元素是否存在都完成了相同的结果.未找到。

我能做些什么来纠正它?

【问题讨论】:

    标签: javascript node.js rest express


    【解决方案1】:

    checkIfPKExists() 是一个异步函数,如果你想使用 ch 你必须使用 .then() 在函数中获取它然后使用价值。

    function checkIfPKExists(idVal)
    {
        return pkdb.get(idVal).then(function(value)
        {
            if(value)
            {
                return true;}
            else
            {   
                return false;
            }           
        }, function(err)
        {   console.log(err);
                return false;
        })  
    }
    
     /** UPDATE METHOD **/
     checkIfPKExists("p_k"+req.body.id).then(function(ch){
        if(!ch)
        {
            res.send("pk does not exist oo " + req.body.id);
        }
        else
        {
            return pkdb.put("p_k"+req.body.id, req.body.pk).then(
                function()
                {
    
                    res.send(JSON.stringify(req.body.pk) + "Updated Successfully");
    
                },
                function(err)
                {
    
                    res.send("Error occurred : " + err);
                })
    }})
    

    【讨论】:

    • 注意,Answer 代码处的 checkIfPKExists() 函数调用没有返回值
    • @marvel308 它回答了我的问题。我赞成答案,但没有反映,因为我的代表
    【解决方案2】:

    一个问题是returned 没有来自checkIfPKExists() 函数调用,请参阅Why is value undefined at .then() chained to Promise?。使用.then().catch()获取函数返回的Promise

    function checkIfPKExists(idVal) {
      // `return` the `Promise` here
      return pkdb.get(idVal).then(function(value) {
        if (value) {
          return true;
        } else {
          return false;
        }
      }, function(err) {
        console.log(err);
        return false;
      })
    }
    
    /** UPDATE METHOD **/
    var ch = checkIfPKExists("p_k" + req.body.id);
    
    ch.then(function(bool) {
    // if `true` do stuff
    if (bool) {
      var pk_promise = pkdb.put("p_k" + req.body.id, req.body.pk)
    
      return pk_promise.then(function() {
        return res.send(JSON.stringify(req.body.pk) + "Updated Successfully");
      }, function(err) {
        return res.send("Error occurred : " + err);
      })
    } else {
      // do other stuff
      return res.send("pk does not exist oo " + req.body.id);
    })
    .catch(function(err) {
      // handle error
    })
    

    【讨论】:

    • 感谢您的回答。它确实帮助我更好地理解了 Promise 处理。代码流也不错。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-16
    • 1970-01-01
    • 2016-11-19
    • 2016-02-06
    • 2018-09-25
    • 1970-01-01
    相关资源
    最近更新 更多