Javascript for loop wait for callback - javascript

I have this function:
function tryStartLocalTrendsFetch(woeid) {
var userIds = Object.keys(twitClientsMap);
var isStarted = false;
for (var i = 0; i < userIds.length; i++) {
var userId = userIds[i];
var twitClientData = twitClientsMap[userId];
var isWoeidMatch = (woeid === twitClientData.woeid);
if (isWoeidMatch) {
startLocalTrendsFetch(woeid, twitClientData, function (err, data) {
if (err) {
// Couldn't start local trends fetch for userId: and woeid:
isStarted = false;
} else {
isStarted = true;
}
});
// This will not obviously work because startLocalTrendsFetch method is async and will execute immediately
if (isStarted) {
break;
}
}
}
console.log("No users are fetching woeid: " + woeid);
}
The gist of this method is that I want the line if (isStarted) { break; } to work. The reason is that if it's started it should not continue the loop and try to start another one.
I'm doing this in NodeJS.

try to use a recursive definition instead
function tryStartLocalTrendsFetch(woeid) {
var userIds = Object.keys(twitClientsMap);
recursiveDefinition (userIds, woeid);
}
function recursiveDefinition (userIds, woeid, userIndex)
var userId = userIds[userIndex = userIndex || 0];
var twitClientData = twitClientsMap[userId];
var isWoeidMatch = (woeid === twitClientData.woeid);
if (isWoeidMatch && userIndex<userIds.length) {
startLocalTrendsFetch(woeid, twitClientData, function (err, data) {
if (err) {
recursiveDefinition(userIds, woeid, userIndex + 1)
} else {
console.log("No users are fetching woeid: " + woeid);
}
});
} else {
console.log("No users are fetching woeid: " + woeid);
}
}

You may also use async (npm install async):
var async = require('async');
async.forEach(row, function(col, callback){
// Do your magic here
callback(); // indicates the end of loop - exit out of loop
}, function(err){
if(err) throw err;
});
More material to help you out: Node.js - Using the async lib - async.foreach with object

Related

I keep getting this Error: Cannot modify a WriteBatch that has been committed

I am trying to commit to batch but I keep getting this error but cant figure out where i am going wrong, i tried moving the batch outside certain functions but still not progress, here is my code,i am not sure what I'm missing , i keep getting this error :
scheduledFunction
Error: Cannot modify a WriteBatch that has been committed.
at WriteBatch.verifyNotCommitted (/workspace/node_modules/#google-cloud/firestore/build/src/write-batch.js:117:19)
at WriteBatch.update (/workspace/node_modules/#google-cloud/firestore/build/src/write-batch.js:313:14)
at /workspace/index.js:152:48
at QuerySnapshot.forEach (/workspace/node_modules/#google-cloud/firestore/build/src/reference.js:748:22)
at step2 (/workspace/index.js:91:39)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
here is my code
async function updateBets() {
var marketRef = db.collection('matches');
var snapshot = await marketRef.where('matchStatus', '==', 'FINISHED').get();
if (snapshot.empty) {
console.log('No matching documents.');
return;
}
console.log('I found documents');
snapshot.forEach(doc => {
if (doc.data().matchId == null) {
//console.log('document with error" ' + doc.id);
}
step2();
async function step2() {
if (doc.data().matchId != null) {
var query = doc.data().matchId.toString();
// console.log('here is match ID' + query);
var marketRef2 = db.collection('markets');
var snapshot2 = await marketRef2.where('marketId', '==', query).get();
snapshot2.forEach(doc2 => {
if (doc2.data().marketTitleId == 'FULL_TIME_RESULT') {
var a = doc.data().homeTeamScore;
var b = doc.data().awayTeamScore;
var winnerIndex;
if (a > b) {
winnerIndex = 0;
var resultIndex = ['WINNER', 'LOSER', 'LOSER'];
var docName = `${doc.data().matchId}` + '000' + '1';
var sfRef = db.collection('markets').doc(docName);
batch5.update(sfRef, {
results: resultIndex
});
} else if (a == b) {
winnerIndex = 1;
var docName = `${doc.data().matchId}` + '000' + '1';
var resultIndex = ['LOSER', 'WINNER', 'LOSER'];
var sfRef = db.collection('markets').doc(docName);
batch5.update(sfRef, {
results: resultIndex
});
} else if (a < b) {
winnerIndex = 2;
var docName = `${doc.data().matchId}` + '000' + '1';
var resultIndex = ['LOSER', 'LOSER', 'WINNER'];
var sfRef = db.collection('markets').doc(docName);
batch5.update(sfRef, {
results: resultIndex
});
}
}
})
}
}
});
try {
await batch5.commit();
console.log("im done with results");
} catch (err) {
console.log('Mac! there was an error with results: ', err);
}
}
.....................................................................................................
Your main function is an async which will continue to run all methods asynchronously, ignoring any internal functions. You must make sure that all jobs being applied to the batch are added before attempting to commit, at the core this is a race condition.
You can handle these multiple ways, such as wrapping it with a promise chain or creating a callback.
For a callback, add the following before your forEach
var itemsProcessed = 0;
Insert this at the end of the forEach
itemsProcessed++;
if(itemsProcessed === array.length) {
CommitBatch();
}
Then put your batch.commit() method inside this callback function
function CommitBatch(){
try {
await batch5.commit();
console.log("im done with results");
} catch (err) {
console.log('Mac! there was an error with results: ', err);
}
}

how to call an array which is in a function from the another function in javascript?

This is the async function:
async function getpackages(conn, childId, callback) {
var childId = childId;
var request6 = new sql.Request(conn);
var packageQuery = "select OrderId,ChildID from dbo.tbl_Scheduler where NoOfMealsLeft>0 and ChildId=" + childId;
await request6.query(packageQuery, function (err, packagelist) {
if (!err && packagelist.recordsets.length > 0) {
console.log("Error:" + err + "Result:" + util.inspect(packagelist.recordsets[0]));
var orderdetail_ = [];
for (i = 0; i < packagelist.recordsets[0].length; i++) {
orderdetail_.push(packagelist.recordsets[0][i].OrderId);
}
console.log("-->" + orderdetail_);
callback(null, packagelist.recordsets[0]);
} else if (packagelist.recordsets.length < 1) {
callback("Not a valid id input", null);
}
});
};
I need to call the orderdetails_ array in the query. The array contains four data and I need to iterate over 4 data one by one, using the or in the SQL query.
module.exports.newscheduledmeal = function (req, res, next, callback) {
let entered_date = req.query.date;
let childId = req.query.id;
let current_date = new Date().toISOString().slice(0, 10);
if (entered_date < current_date) {
return callback('Please enter date more than or equal to current date.', null);
} else
var conn = new sql.ConnectionPool(dbConfig);
try {
conn.connect().then(function () {
var request = new sql.Request(conn);
getpackages(conn, childId, function (err, orderid) {
if (err) {
callback(err, null);
} else
var PackageidQuery = "select PackageId from dbo.tbl_Order where OrderId=";
request.query(PackageidQuery, function (err, packagelist) {
if (!err) {
conn.close();
callback(null, packagelist.recordsets);
} else {
conn.close();
callback("Error", null);
}
});
});
});
} catch (err) {
console.log("Exception occured:" + err);
conn.close();
callback(err, null);
}
};
I want to get the details of the array which is in getpackages to be used in the module section and specifically in the SQL query section.

Why is my code not waiting for the completion of the function?

I am trying to read some data from a file and store it in a database.
This is part of a larger transaction and I need the returned ids for further steps.
async parseHeaders(mysqlCon, ghID, csv) {
var self = this;
var hIDs = [];
var skip = true;
var idx = 0;
console.log("Parsing headers");
return new Promise(async function(resolve, reject) {
try {
var lineReader = require('readline').createInterface({
input: require('fs').createReadStream(csv)
});
await lineReader.on('close', async function () {
console.log("done: ", JSON.stringify(hIDs));
resolve(hIDs);
});
await lineReader.on('line', async function (line) {
line = line.replace(/\"/g, '');
if (line.startsWith("Variable")) { //Variable,Statistics,Category,Control
console.log("found variables");
skip = false; //Ignore all data and skip to the parameter description.
return; //Skip also the header line.
}
if (!skip) {
var data = line.split(",");
if (data.length < 2) { //Variable section done return results.
console.log("Found sub?",line);
return lineReader.close();
}
var v = data[0];
var bidx = data[0].indexOf(" [");
if (bidx > 0)
v = data[0].substring(0, bidx); //[] are disturbing mysql (E.g.; Air temperature [�C])
var c = data[2];
hIDs[idx++] = await self.getParamID(mysqlCon, ghID, v, c, data);//, function(hID,sidx) { //add data in case the parameter is not in DB, yet.
}
});
} catch(e) {
console.log(JSON.stringify(e));
reject("some error occured: " + e);
}
});
}
async getParamID(mysqlCon,ghID,variable,category,data) {
return new Promise(function(resolve, reject) {
var sql = "SELECT ID FROM Parameter WHERE GreenHouseID="+ghID+" AND Variable = '" + variable + "' AND Category='" + category + "'";
mysqlCon.query(sql, function (err, result, fields) {
if(result.length === 0 || err) { //apparently not in DB, yet ... add it (Acronym and Machine need to be set manually).
sql = "INSERT INTO Parameter (GreenHouseID,Variable,Category,Control) VALUES ("+ghID+",'"+variable+"','"+category+"','"+data[3]+"')";
mysqlCon.query(sql, function (err, result) {
if(err) {
console.log(result,err,this.sql);
reject(err);
} else {
console.log("Inserting ",variable," into DB: ",JSON.stringify(result));
resolve(result.insertId); //added, return generated ID.
}
});
} else {
resolve(result[0].ID); //found in DB .. return ID.
}
});
});
}
The functions above are in the base class and called by the following code:
let headerIDs = await self.parseHeaders(mysqlCon, ghID, filePath);
console.log("headers:",JSON.stringify(headerIDs));
The sequence of events is that everything in parseHeaders completes except for the call to self.getParamID and control returns to the calling function which prints an empty array for headerIDs.
The console.log statements in self.getParamID are then printed afterward.
What am I missing?
Thank you
As you want to execute an asynchronous action for every line we could define a handler to do right that:
const once = (target, evt) => new Promise(res => target.on(evt, res));
function mapLines(reader, action) {
const results = [];
let index = 0;
reader.on("line", line => results.push(action(line, index++)));
return once(reader, "close").then(() => Promise.all(results));
}
So now you can solve that easily:
let skip = false;
const hIDs = [];
await mapLines(lineReader, async function (line, idx) {
line = line.replace(/\"/g, '');
if (line.startsWith("Variable")) { //Variable,Statistics,Category,Control
console.log("found variables");
skip = false; //Ignore all data and skip to the parameter description.
return; //Skip also the header line.
}
if (!skip) {
var data = line.split(",");
if (data.length < 2) { //Variable section done return results.
console.log("Found sub?",line);
return lineReader.close();
}
var v = data[0];
var bidx = data[0].indexOf(" [");
if (bidx > 0)
v = data[0].substring(0, bidx); //[] are disturbing mysql (E.g.; Air temperature [�C])
var c = data[2];
hIDs[idx] = await self.getParamID(mysqlCon, ghID, v, c, data);
}
});

Promise function is not running funcation after .then

What I am trying to do is run functions that will send out messages. The problem with node js is that it does not return in sequence. I do however want each function to run in the sequence that it is coded.
Not really sure what I'm missing here.
Right now I can get it to return only message 1 and message 2 in sequence BUT I want to go all the way from callfirstmessage to callfifthmessage. When I have tried to do this they all get returned out of order.
var promise = new Promises(function(resolve, reject) {
// do some async stuff
callfirstmessage();
//resolve(data);
if (success) {
resolve(data);
} else {
reject(reason);
}
});
promise.then(function(data) {
// function called when first promise returned
return new Promises(function(resolve, reject) {
// second async stuff
callthirdmessage();
//resolve(data);
if (success) {
resolve(data);
} else {
reject(reason);
}
});
}, function(reason) {
// error handler
}).then(function(data) {
callsecondmessage();
}, function(reason) {
// second error handler
});
I have looked at a few different posts talking about this topic and I have used those suggestions. I think my issue here is not being able to successfully chain these together. I have spent a few days on this with no additional progress...I need help bad!
-----------------------edit----------------------------
I think I need to apply something similar to this .then I have found here: https://coderwall.com/p/ijy61g/promise-chains-with-node-js. But for some reason it is still sending messages at random.
Maybe, I'm just going in the completely wrong direction. :/
Below is everything I am working with
// dependencies
console.log("starting program");
var async = require('async');
var AWS = require('aws-sdk');
var Converter = require("csvtojson").Converter;
var util = require('util');
var Promises = require('pinkie-promise');
var vow = require('vow');
var s3 = new AWS.S3();
var snsTopicEMAIL = new AWS.SNS({
params: {
TopicArn: 'arn:aws:sns:xxxxxxxxxxxxxxxxxxxxxxxx'
}
});
var snsTopicSMS = new AWS.SNS({
params: {
TopicArn: 'arn:aws:sns:xxxxxxxxxxxxxxxxxxxxxxxxx'
}
});
var abort = false;
//SMS
function topDSRsms(csvJSON) {
var ytdTopReg = "";
if (csvJSON.length < 1) {
console.error("Not enough data records in CSV to compose an SMS. Aborting. count:" + csvJSON.length);
tdTopReg = "There was an error. Please contact IT for assistance.";
abort = true;
throw '';
}
ytdTopReg = "";
for (var i = 0; i < csvJSON.length; i++) {
var csvItem = csvJSON;
if (csvItem[i].KPIvalue.length > 0 && csvItem[i].KPI.length > 0) {
var KPI = " " + csvItem[i].KPI + ":" + csvItem[i].KPIvalue + "\n";
} else {
KPI = " ";
}
if (csvItem[i].TrendValue1.length > 0 && csvItem[i].Trend1.length > 0) {
var trend1 = "\n" + svItem[i].Trend1 + ":" + csvItem[i].csvItem[i].TrendValue1;
} else {
trend1 = "";
}
if (csvItem[i].TrendValue2.length > 0 && csvItem[i].Trend2.length > 0) {
var trend2 = "\n" + csvItem[i].Trend2 + ":" + csvItem[i].TrendValue2;
} else {
trend2 = "";
}
if (csvItem[i].TrendValue3.length > 0 && csvItem[i].Trend3.length > 0) {
var trend3 = "\n" + csvItem[i].Trend3 + ":" + csvItem[i].TrendValue3;
} else {
trend3 = "";
}
ytdTopReg += "\n#" + (i + 1) + KPI + csvItem[i].RankTrend + ":" + csvItem[i].RankTrendValue + trend1 + trend2 + trend3;
}
return ytdTopReg;
}
function dshSmsStr(csvJSON) {
return "DSR:" +
topDSRsms(csvJSON);
}
//email
function topDSREmail(csvJSON) {
var ytdTopReg = "";
ytdTopReg = "Top orders By Region - ";
for (var i = 0; i < csvJSON.length; i++) {
if (csvJSON.length < 1) {
console.error("Not enough data records in CSV to compose an email. Aborting.");
ytdTopReg = "There was an error. Please contact IT for assistance."
abort = true;
throw '';
//return -1;
} else {
var csvItem = csvJSON;
if (csvItem[i].KPIvalue.length > 0 && csvItem[i].KPI.length > 0) {
var KPI = " " + csvItem[i].KPI + ":" + csvItem[i].KPIvalue + "\n";
} else {
KPI = "";
}
if (csvItem[i].TrendValue1.length > 0 && csvItem[i].Trend1.length > 0) {
var trend1 = "\n" + svItem[i].Trend1 + ":" + csvItem[i].csvItem[i].TrendValue1;
} else {
trend1 = "";
}
if (csvItem[i].TrendValue2.length > 0 && csvItem[i].Trend2.length > 0) {
var trend2 = "\n" + csvItem[i].Trend2 + ":" + csvItem[i].TrendValue2;
} else {
trend2 = "";
}
if (csvItem[i].TrendValue3.length > 0 && csvItem[i].Trend3.length > 0) {
var trend3 = "\n" + csvItem[i].Trend3 + ":" + csvItem[i].TrendValue3;
} else {
trend3 = "";
}
ytdTopReg += "\n#" + (i + 1) + KPI + csvItem[i].RankTrend + ":" + csvItem[i].RankTrendValue + trend1 + trend2 + trend3 + "\n";
}
}
return ytdTopReg;
}
function dshEmailStr(csvJSON) {
return "DSR:" +
topDSREmail(csvJSON);
}
console.log("compiled totals string and customer string");
exports.handler = function(event, context) {
// Read options from the event.
console.log("Reading options from event:\n", util.inspect(event, {
depth: 5
}));
var srcBucket = event.Records[0].s3.bucket.name;
console.log("got src bucket name");
// Object key may have spaces or unicode non-ASCII characters.
var srcKey = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " "));
console.log("src key decoded");
// Download the CSV file from S3, Extract&Load data into DynamoDB, and notify end users.
async.waterfall([
function downloadCSV(next) {
// Download the Daily Sales Highlights CSV file from S3 into a buffer.
s3.getObject({
Bucket: srcBucket,
Key: srcKey
},
next);
},
function parseCSV(response, next) {
console.log(response.ContentType);
var csvStream = AWS.util.buffer.toStream(response.Body);
var csvJSON = [];
var converter = new Converter({});
//end_parsed will be emitted once parsing finished
converter.on("end_parsed", function(jsonArray) {
console.log(jsonArray); //here is your result jsonarray
csvJSON = jsonArray;
next(null, csvJSON);
});
//read CSV from stream
csvStream.pipe(converter);
},
function updateState(csvJSON, next) {
//Update state in DynamoDB
next(null, csvJSON);
},
function notifyEndUsers(csvJSON, next) {
//Notify end users
console.log("\n\nNotifying SMS end users...\n\n");
console.log(dshSmsStr(csvJSON));
console.log("\n\nNotifying email end users...\n\n");
console.log(dshEmailStr(csvJSON));
//email
snsTopicEMAIL.publish({
Message: dshEmailStr(csvJSON)
}, function(err, data) {
if (err) {
console.log(err.stack);
next(err);
}
console.log(data);
next(null);
});
//SMS
var datastr = dshSmsStr(csvJSON);
var strlen = dshSmsStr(csvJSON).length;
var ranscript = false;
var completemsg1 = false;
var completemsg2 = false;
var completemsg3 = false;
var completemsg4 = false;
var completemsg5 = false;
var completemsg6 = false;
function callfirstmessage() {
snsTopicSMS.publish({
Message: dshSmsStr(csvJSON).substring(0, 150)
}, function(err, data) {
if (err) {
console.log(err.stack);
next(err);
}
console.log(data);
next(null);
});
return true;
}
function callsecondmessage() {
snsTopicSMS.publish({
Message: dshSmsStr(csvJSON).substring(150, 300)
}, function(err, data) {
if (err) {
console.log(err.stack);
next(err);
}
console.log(data);
next(null);
});
return true;
}
function callthirdmessage() {
snsTopicSMS.publish({
Message: dshSmsStr(csvJSON).substring(300, 450)
}, function(err, data) {
if (err) {
console.log(err.stack);
next(err);
}
console.log(data);
next(null);
});
return true;
}
function callfourthmessage() {
snsTopicSMS.publish({
Message: dshSmsStr(csvJSON).substring(450, 600)
}, function(err, data) {
if (err) {
console.log(err.stack);
next(err);
}
console.log(data);
next(null);
});
return true;
}
function callfifthmessage() {
snsTopicSMS.publish({
Message: dshSmsStr(csvJSON).substring(600, 750)
}, function(err, data) {
if (err) {
console.log(err.stack);
next(err);
}
console.log(data);
next(null);
});
return true;
}
function one() {
new Promises(function(resolve1, reject) {
completemsg1 = callfirstmessage();
if (completemsg1 === true) {
resolve1(true);
console.log('first success');
} else {
reject(false);
console.log('first fail');
}
});
}
function two() {
new Promises(function(resolve2, reject) {
completemsg2 = callsecondmessage();
if (completemsg2 === true) {
resolve2(completemsg2);
console.log('second success');
} else {
reject(false);
console.log('second fail');
}
});
}
function three() {
new Promises(function(resolve3, reject) {
completemsg3 = callthirdmessage();
if (completemsg3 === true) {
resolve3(completemsg3);
console.log('third success');
} else {
reject(false);
console.log('third fail');
}
});
}
function four() {
new Promises(function(resolve4, reject) {
completemsg4 = callfourthmessage();
if (completemsg4 === true) {
resolve4(completemsg4);
console.log('fourth success');
} else {
reject(false);
console.log('fourth fail');
}
});
}
function five() {
new Promises(function(resolve5, reject) {
completemsg5 = callfifthmessage();
if (completemsg5 === true) {
resolve5(completemsg5);
console.log('fifth success');
} else {
reject(false);
console.log('fifth fail');
}
});
}
console.log("\n\n Begin SMS script. Data length :" + strlen + "...\n\n");
if (strlen < 150) { //one message sent
//Will complete later
ranscript = true;
} else if (strlen < 300) { //two messages sent
//Will complete later
ranscript = true;
} else if (strlen < 450) { //three messages sent
//Will complete later
ranscript = true;
} else if (strlen < 600) { //four messages sent
one().then(function() {
return two();
})
.then(function() {
return three();
})
.then(function() {
return four();
})
.then(function() {
return five();
});
ranscript = true;
} else if (strlen < 750) { //five messages sent
//Will complete later
ranscript = true;
} else {
console.log("\n\n The SMS mesages is too large to send to end users...\n\n");
}
if (ranscript === false) {
console.log("\n\n SMS script did not run \n\n");
} else if (ranscript === true) {
console.log("\n\n SMS script successfully ran \n\n");
}
}
], function(err) {
if (err) {
console.error(
'Unable to process due to an error: ' + err
);
} else {
console.log(
'Successfully processed'
);
}
context.done();
});
}
The executor for the first promise (reformatted):
var promise = new Promise(function(resolve, reject)
{ // do some async stuff
callfirstmessage();
if (success)
{ resolve(data);
} else
{ reject(reason);
}
});
doesn't resolve or reject the returned promise asynchronously. You will need to get a call back from the asynchronous operation before resolving/rejecting the returned promise. In pseudo code something more like:
var promise = new Promise(function(resolve, reject)
{ // do some async stuff
callfirstmessage( successCallBack, failCallBack);
function successCallBack( data)
{ resolve(data);
}
function failCallBack( reason)
{ reject(reason);
}
});
In reality you may have to use a call back the asynchronous operation supports, and then analyze success or failure of the operation depending on a status property located in a request or response object.

How to use global error handling code in node.js for entire api call

I have a api call which has more than one functions. Instead of applying error-handling for each and every method, is it possible to use global error handling code that send the error to UI developers.
The code is given below:
app.post('/billing/pricingdetails', function (req, res) {
console.log('pricing api called');
var workload = req.body;
var resourcelevelPricing = {};
var response = {};
var workloadinfo = {
workloadId: workload.workloadId,
ownerId: workload.ownerId,
uniqueName: workload.uniqueName,
name: workload.name
}
var pricing = {}
var allresourceIdentifiers;
if (workload.elements && workload.elements.length > 0) {
var elementlevelpricingSummary = {};
var elementArray = [];
var allresourceIdentifierArray = [];
var elementinfo = {};
var metadataModified = {};
var elementsParam = workload.elements;
// handle configurable resource
var configurableElementarray = [];
// create array of all the elements in workloadjson - to be used for resourcelevel (instance/image), charamountunitlevel, resourcetypelevel pricing detail
for (var index in elementsParam) {
// if condition skips the uri of configurable resources - handle configurable resource
if(!elementsParam[index].parameters.ResourceParameters)
{
allresourceIdentifierArray.push(elementsParam[index].uri);
if (elementsParam[index].parameters.imageUri) {
allresourceIdentifierArray.push(elementsParam[index].parameters.imageUri);
}
}
}
var allresourceIdentifiers = allresourceIdentifierArray.join(',');
// call the functionalities that gives the each level of pricing detail synchronously to construct the workload json
async.series([
function (callback) {
getpricingSummary(elementsParam, function (err, workloadinfo) {
if(err){
}
else
{
callback(null, workloadinfo);
}
});
},
function (callback) {
getPricingforResourceIdentifiers(allresourceIdentifiers, function (err, pricingDetail) {
pricing.resourceLevel = pricingDetail;
callback(null, pricingDetail);
});
},
function (callback) {
getchargeamountunitlevelPricing(allresourceIdentifiers, function (err, pricingDetail) {
//merge configurable resource with concrete resource pricing details - handle configurable resource
if(configurableElementarray.length > 0)
{
var concatednatedArray = pricingDetail.concat(configurableElementarray);
var finalResult = [];
var i = concatednatedArray.reduce(function (result, o) {
var key = o.chargeAmountUnit + o.currencyCode;
if (!(key in result)) {
result.arr.push(result[key] = o);
finalResult.push(result);
}
else {
result[key].chargeAmount += Number(o.chargeAmount);
}
return result;
}, { arr: [] }).arr;
pricing.chargeamountunitLevel = i;
trace.info(i);
}
else
{
pricing.chargeamountunitLevel = pricingDetail;
}
callback(null, pricingDetail);
});
},
function (callback) {
getresourcetypelevelPricing(allresourceIdentifiers, function (err, pricingDetail) {
if(configurableElementarray.length > 0)
{
var concatednatedArray = pricingDetail.concat(configurableElementarray);
var i = concatednatedArray.reduce(function (result, o) {
var key = o.chargeAmountUnit + o.currencyCode + o.Name;
if (!(key in result)) {
result.arr.push(result[key] = o);
}
else {
result[key].chargeAmount += o.chargeAmount;
}
return result;
}, { arr: [] }).arr;
pricing.resourcetypeLevel = i;
trace.info(i);
}
else
{
pricing.resourcetypeLevel = pricingDetail;
}
callback(null, pricingDetail);
});
}
],
function (err, result) {
workloadinfo.pricing = pricing;
res.send(workloadinfo);
});
// get element level pricing summary for each elements (vm/vs) in the array within workload json - the output to be appended within metadata of workload json
function getpricingSummary(elementsParam, callback) {
async.forEachSeries(elementsParam, createResponse, function (err,result) {
return callback(null, result);
});
};
// this method called by async.forEachSeries passing each elements (vm/vs) of workload
function createResponse(elements, callback) {
var resourceIdentifierArray = [];
elementinfo = elements;
resourceIdentifierArray.push(elements.uri);
if (elements.parameters.imageUri) {
resourceIdentifierArray.push(elements.parameters.imageUri);
}
// build string of resourceIdentifier (instance/image) for input element
var resourceIdentifiers = resourceIdentifierArray.join(',');
console.log(resourceIdentifiers);
if(elements.parameters.ResourceParameters)
{
trace.info('1');
trace.info(elements.parameters.ResourceParameters);
var configJson = JSON.parse(elements.parameters.ResourceParameters);
trace.info(Number(configJson.cpuCount));
metadataModified = elements.metadata;
// TODO : Remove this hard-coding
elementlevelpricingSummary.Name = 'Hardware';
if(configJson.totalUnitPrice)
{
var chargeAmount = configJson.totalUnitPrice;
elementlevelpricingSummary.chargeAmount = Math.round(chargeAmount * 100)/100;
}
if(configJson.ChargeAmountUnit)
{
var chargeAmountUnit = configJson.ChargeAmountUnit;
elementlevelpricingSummary.chargeAmountUnit = configJson.ChargeAmountUnit;
}
if(configJson.CurrencyCode)
{
var currencyCode = configJson.CurrencyCode;
elementlevelpricingSummary.currencyCode = configJson.CurrencyCode;
}
metadataModified.pricingSummary = elementlevelpricingSummary;
configurableElementarray.push(elementlevelpricingSummary);
// delete original metadata from workload json (to be replaced by metadata containing pricing summary)
delete elementinfo.metadata;
elementinfo.metadata = metadataModified;
elementArray.push(elementinfo);
// global workloadinfo variable is appended with array of elements with its pricing summary within metadata of respective elements
workloadinfo.elements = elementArray;
return callback();
}
else
{
// Get element level pricing summary
mysql.elementlevelpricing(resourceIdentifiers, conn, function (result) {
elementlevelpricingSummary = result;
metadataModified = elements.metadata;
metadataModified.pricingSummary = elementlevelpricingSummary;
// delete original metadata from workload json (to be replaced by metadata containing pricing summary)
delete elementinfo.metadata;
elementinfo.metadata = metadataModified;
elementArray.push(elementinfo);
// global workloadinfo variable is appended with array of elements with its pricing summary within metadata of respective elements
workloadinfo.elements = elementArray;
return callback(null,workloadinfo);
});
}
};
function getPricingforResourceIdentifiers(resourceIdentifiers, callback) {
mysql.pricingDetail(resourceIdentifiers, conn, function (result) {
return callback(null, result);
});
};
function getchargeamountunitlevelPricing(resourceIdentifiers, callback) {
mysql.chargeamountunitlevelPricing(resourceIdentifiers, conn, function (result) {
return callback(null, result);
});
};
function getresourcetypelevelPricing(resourceIdentifiers, callback) {
mysql.resourcetypelevelPricing(resourceIdentifiers, conn, function (result) {
return callback(null, result);
});
};
};
});
With Express, you can install an error handler which will be called when an error occurs in any of your routes:
// somewhere at the end of your middleware/route chain
app.use(function(err, req, res, next) {
res.send(500, err.message); // or whatever you want to send back
});
It would still be best to rethrow any errors that occur in your code:
if (err) throw err;
Also, since you're using async, you can always propagate errors back to it:
if (err) return callback(err);
And handle the errors in the final callback.

Categories

Resources