Promise handling - update db entry if exists - javascript

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:
API calls for update method handler on node js with ID and Set of values (json)
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.
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?

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
})

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

Related

NodeJS Async: callback allready called?

I am getting this error:
node:19100) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Callback was already called.
On my async.each call, it seems like it is trying to call "done()" more then once per "circuit" but I don't understand why, i though that once the async callback is called the function would exit ?
Circuits is an array of String containing ids. I am simply trying to loop through them to execute async calls to database on each.
var getTimeseriesForCircuit = function(circuits, to, from, callback){
if (!validateDates(from, to)) {
return callback(400, 'Invalid date(s)');
}
var validJSON = false;
try {
circuits = JSON.parse(circuits);
validJSON = true;
}
catch (e) {
return callback(500, e);
}
if (validJSON) {
async.each(circuits, function (circuitID, done) {
var frequency = getFrequencyFromRange(from, to);
var influxFromDate = from * 1000000;
var influxToDate = to * 1000000;
getVoltageGatewayID(null, circuitID, function (gatewayID) {
getVoltageFromInflux(null, influxFromDate, influxToDate, gatewayID, frequency, function (voltage) {
getMeanAmpsFromInflux(null, influxFromDate, influxToDate, circuitID, frequency, function (data) {
if (JSON.stringify(data) != []) {
var timeWithPower = calculatePower(data, voltage);
return done(null, {'circuitID': circuitID, data: timeWithPower});
}
});
});
});
}, function (err, results) {
if (err) {
return callback(500, err)
} else {
return callback(200, results)
}
});
}
else {
return callback(400, 'The circuits sent were not in a valid format');
}
}
I think you have to call your async callback "done" without return:
done(null, {'circuitID': circuitID, data: timeWithPower});
and on error something like this:
done('errormessage');
so you get your result in the "final"callback after your each
see async
I think you missing return statement in function.
When you catch or have an error instead of just callback() use return callback().
This will prevent execution of code bellow return statements and error that you see.
Hope this helps.

Series control flow with bluebird promises

I have a number of promises and I want to execute them in order, but conditionally.
For example, I have the following promises:
getItems()
getItemsSource()
getItemsAlternativeSource()
What I want to do is try getItems() first. If this resolves with an empty value OR if it throws an error, I want to log that error (if that's the case), but then try getItemsSource(), same as above, if it resolves with no value or throws an error, I want to log the error if that's the case and try getItemsAlternativeSource().
I know I can do this conditionally, in each then() or catch(), but that seems a bit redundant. Is there a better way to handle this kind of control flow?
Thank!
You can use an empty value as the return value of the catch handler:
getItems().catch(function(err) {
console.warn(err);
return null; // <==
}).then(function(items) {
if (items) return items;
else return getItemsSource().catch(function(err) {
console.warn(err);
return null; // <==
}).then(function(sourceitems) {
if (items) return items;
else return getItemsAlternativeSource().catch(function(err) {
console.warn(err);
throw new Error("items couldn't be fetched normally, from source, or from alternative source");
});
});
});
If you absolutely want to avoid duplication, you can use this highly abstract approach:
var tryAll = [getItems, getItemsSource, getItemsAlternativeSource].reduceRight(function(nextAlternative, fetch) {
return function() {
return fetch().then(function(items) {
if (items) return items;
else return nextAlternative(); // now we can even call it in two locations
}, function(err) {
console.warn(err);
return nextAlternative(); // without having to resort to catch-then
});
};
}, function last() {
throw new Error("items couldn't be fetched normally, from source, or from alternative source");
});
tryAll();
I'd suggest you create a function that takes an array of functions that will call each function in the array until one returns with some data.
function getFirstData(array) {
var index = 0;
function next() {
if (index < array.length) {
return array[index++]().then(function(data) {
// if we got an answer, return it as the resolve value
if (data) return data;
// otherwise, reject so we go to the next one
return Promise.reject(null);
}).catch(function(err) {
if (err) console.err(err);
return next();
});
} else {
// got to the end of the array without a value
throw new Error("No data found");
}
}
return Promise.resolve().then(next);
}
var fns = [getItem, getItemsSource, getItemsAlternativeSource];
getFirstData(fns).then(function(data) {
// got data here
}).catch(function(err) {
// no data found here
});
If you want the functions to have arguments, then you can .bind() the arguments to the functions before putting them in the array.
And, here's a different implementation using .reduce() to traverse the array:
function getFirstData(array) {
return array.reduce(function(p, fn) {
return p.then(function(priorData) {
// if we already got some data, then just return it
// don't execute any more functions
if (priorData) return priorData;
return fn().catch(function(err) {
console.log(err);
return null;
});
});
}, Promise.resolve()).then(function(data) {
if (!data) {
throw new Error("No data found");
}
});
}

Synchronize call back in Angular JS

I am trying synchronize two call backs one from service and one directly called . (This is old code so please ignore code quality)
if($scope.newUser.password == $scope.newUser.password2)
{
// search for the user to see if it already exists
UserValidateService.findUserByEmail($scope.newUser.username, $scope.newUser.email, function(user){
console.log(user);
if(user[0]) {
alert('email Id already exists ******');
return false;
}
}).then(function(){
$http.get("/api/user/"+$scope.newUser.username)
.success(function(newUser) {
// if user does not exist, create it new
if(newUser.length == 0) {
$http.post("/api/user", $scope.newUser)
.success(function(newUser){
if(newUser == null)
alert("Unable to register user");
else
$location.path( $scope.newUser.username+"/home" );
});
}
else
{
alert("User already exists");
}
});
});
}
else
{
alert("Passwords must match. Try again");
}
Service :
app.factory('UserValidateService', function($http){
var findUserByEmail = function (user, email, callback) {
$http.get("/api/user/"+user+"/email/"+email)
.success(callback);
};
return {
findUserByEmail: findUserByEmail
};
});
Error:
TypeError: Cannot read property 'then' of undefined
at h.$scope.register (user.js:118)
at angular.js:10567
at angular.js:18627
at h.$eval (angular.js:12412)
at h.$apply (angular.js:12510)
at HTMLButtonElement.<anonymous> (angular.js:18626)
at HTMLButtonElement.jQuery.event.dispatch (jquery.js:5095)
at HTMLButtonElement.elemData.handle (jquery.js:4766)
Can any please let me where I am going wrong or how to make this code synchronize i.e calling
Your UserValidateService doesn't return a promise, it uses a callback from the $http success method. This is an anti-pattern that should be avoided.
Convert your factory to return the promise from the $http service:
app.factory('UserValidateService', function($http){
var findUserByEmail = function (user, email) {
return $http.get("/api/user/"+user+"/email/"+email)
};
return {
findUserByEmail: findUserByEmail
};
});
Then use the .then method of the promise to execute the callback functionality.
var promise = UserValidateService
.findUserByEmail($scope.newUser.username,
$scope.newUser.email);
var derivedPromise = promise.then ( function(response){
console.log(response.user);
if(response.user[0]) {
alert('email Id already exists ******');
});
//return response for chaining
return response;
});
Subsequent actions can chain off the derivedPromise.

Bluebird Promise Chains: 'Catch' with Result

In order to make this question as useful to as many people as possible, I will exclude my specific implementation details beyond that fact that I am using the Bluebird promise library with Node + Express below.
So, let's say that I have the following chain (where P returns a promise, and res is the Express HTTP response object):
P().then(function(){
// do nothing if all went well (for now)
// we only care if there is an error
}).catch(function(error){
res.status(500).send("An error occurred");
}).then(function(){
return P();
}).then(function(pVal1){
return [pVal1, P()];
}) // TODO: catch an error from P() here and log pVal1
.spread(function(pVal1, pVal2){
if(pVal1 === pVal2) {
console.log("Success!");
} else {
console.log("Failure");
}
});
Where I have placed the TODO comment above is where I would like to catch an error that might occur from my call to P. If I do catch an error, I would like to log pVal1 and then send a 500 error, as is done in the first catch. However, I am not sure if this is possible with how I am structuring my chain.
I believe that I need to do some "branching," but I do not think that I understand this concept well enough to stop the asynchronous nature of JavaScript from getting the best of me! As such, any help is thoroughly appreciated.
Don't forget to catch errors in the end of the chain. That's also the place to send the response.
Catching errors in the middle of a chain is for intermittent error handling; the chain continues to run, so don't send a response just yet.
Here is something to try it out:
// example middleware
function handle(req, res, next) {
log("----------------");
return async("p1", "foo").then(function (pVal1) {
return pVal1;
}).then(function (pVal1) {
var p2a = async("p2a", "bar"),
p2b = async("p2a", "bar").catch(function (error) {
log("Logging: " + error + " (pVal1 " + pVal1 + ")");
});
return [p2a, p2b];
}).spread(function (pVal1, pVal2) {
if (pVal1 === pVal2) {
res.send("Success!");
} else {
res.send("Failure");
}
}).catch(function (error) {
res.status(500).send("An error occurred");
log("Logging: " + error);
});
}
// ---------------------------------------------------------------------
// mockup response object
var res = {
status: function (code) {
log("Sending status: " + code);
return this;
},
send: function () {
log("Sending response: " + [].join.call(arguments, " "));
return this;
}
};
// mockup promise generator
function async(name, value) {
return new P(function (resolve, reject) {
if ( confirm("let " + name + " succeed?") ) {
log(name + " succeeds...");
resolve(value);
} else {
log(name + " fails...");
reject(name + " has failed");
}
});
}
function log() {
var msg = document.createElement("DIV");
msg.textContent = [].join.call(arguments, " ");
document.getElementById("log").appendChild(msg)
document.body.scrollTop = document.body.scrollHeight;
}
button {
position: fixed;
top: 5px;
}
<script src="http://cdnjs.cloudflare.com/ajax/libs/bluebird/2.9.33/bluebird.min.js"></script>
<button onclick="handle(null, res, null)">Go</button>
<div id="log"></div>
This is possible if you use the explicit Promise.all instead of returning an array into .spread.
}).then(function(pVal1){
// this becomes a `Promise.all` - the aggregation is explicit
var all = Promise.all([pVal1, P()]);
all.catch(function(e){ // "branching", we both return and `catch` the promise
console.log("Error, pVal1 is", pVal1);
});
return all; // return it
}).spread(function(pVal1, pVal2){
// ....
});

Returning from anonymous function passed as parameter

Consider the following code:
function dbTask(q) {
mysql = new MySQL();
mysql.host = sqlHost;
mysql.user = sqlUser;
mysql.password = sqlPassword;
mysql.query("USE stock");
return mysql.query(q, function(err, results, fields) {
if(err) {
console.log("MySQL Error: " + err + ", Query: " + q);
return false;
} else {
return results; //here
}
});
};
var r = dbTask("SELECT * FROM user;");
console.log(r);
Whereas, I want the results to be returned from the inner anonymous function when calling dbTask() in the second last line, I am getting different output which seems like some internal construct of the mysql library under use.
How can I get dbTask to return the results when called?
Since mysql.query is asynchronous, then you'll have to re-think your architecture.
Instead of having your function return true or false, you'll have to pass in handlers to be called if the query returned true or false.
Something like this:
function dbTask(q, success, failure) {
mysql = new MySQL();
mysql.host = sqlHost;
mysql.user = sqlUser;
mysql.password = sqlPassword;
mysql.query("USE stock");
mysql.query(q, function(err, results, fields) {
if(err) {
console.log("MySQL Error: " + err + ", Query: " + q);
failure(err, results, fields);
} else {
success(results, fields);
}
});
};
Which you'd call like this:
dbTask(q,
function(results, fields) { /* ... */ },
function(err, results, fields) { /* ... */ });
You can't because dbTask returns once the mysql.query call completes to initiate the query. So by the time mysql.query's callback is called, console.log(r) has already executed.
This is the crux of the asynchronous nature of node.js.
What you can do is have dbTask accept a callback parameter that the function calls once results is available so that it can be provided to the caller asynchronously.

Categories

Resources