2

I am stuck with new challenge on Promises.

Goal: Update the DB entry only if P_KEY exists.

current db is exposed through module and module has get and put method for db. Both returning Promise.

Approach:

  1. API calls for update method handler on node js with ID and Set of values (json)
  2. In handler for post method call to get method of db module check if value on promise success is empty or not if yes return false else true.
  3. If true; data exists call to put method of db module.

but somehow data it always returns false. even if db entry has been made through db api.

/** 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);
    }
  )
}

My understanding is ch value is set from checkPK function and since thats a promise it just goes ahead and processes if loop which by default stands true and done irrespective whether element is there or not same result. Not found.

What can I do to correct it?

Tim Han
  • 166
  • 1
  • 12

2 Answers2

0

checkIfPKExists() is an asynchronous function, if you want to use ch you would have to use a .then() to get it in a function and then use the value.

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);
            })
}})
marvel308
  • 10,288
  • 1
  • 21
  • 32
  • Note, no value is returned from `checkIfPKExists()` function call at code at Answer – guest271314 Aug 12 '17 at 06:21
  • @marvel308 It answers my question. I upvoted the answer but not reflecting since I'm <15 on rep. I appreciate your time and help. Thank you! –  Aug 12 '17 at 09:07
0

One issue is that no value is returned from checkIfPKExists() function call, see Why is value undefined at .then() chained to Promise?. Use .then() and .catch() to get the Promise value returned from the function

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
})
guest271314
  • 1
  • 15
  • 104
  • 177
  • Thank you for your answer. It really helped me understand promise handling better. Nice code flow as well. –  Aug 12 '17 at 09:08