Here’s the code so you can see if there is something unholy in my execution.all other functions have the same db to work with, so go figure. This is not that complicated a function, I don’t think. I call a function to update the DB because I found that the same code, if placed in the function below, did not work reliably to update the DB. This way, as long as the function runs correctly, it should update all rows of the DB that it is supposed to.
Thank you,
function sumprofitloss(){
//this button caculates the sum of the profit/loss column
return new Promise(resolve => {
$w("#dataset1").onReady( () => {
console.log("summing profit/loss now");
let count = $w("#dataset1").getTotalCount(); //get total number of items in DB
console.log("total count for summing profit/loss now", count);
var num = count.toString();
//$w("#text77").text = num;
///start of loop
wixData.query("DailyBetList")
.limit (1000)
.ascending("_createdDate")
.find()
.then( (results) => {
let totalCount = results.totalCount;
let allItems = results.items;
console.log("this is total count for sum of profit loss column ",totalCount);
//var countResults = 0; //setting these two as global variables to avoid using page interface for data management
//var countWinners = 0;
var sumProfitLoss = 0;
var value1ProfitLoss = 0;
var num5 = " ";
var length1 = 4; //prepare to truncate string
var myString1 = " ";
var myTruncatedString1 = " ";
let valueRowNumber = 0;
allItems.forEach((record) => {
let Id = record._id;
let value = record.hiddenFinalResults;
let valueProfitLoss = record.profitLoss;
let valueRowNumber1 = Number(record.hiddenRowCount);
valueRowNumber = valueRowNumber + 1;
console.log ("this is value row number NEW", valueRowNumber)
/////////////////////////////////////////////////////
//////////////////////////////////////////////////////
//get the value of the item in hidden results column for counting winners and bets predicted as a number
var value2 = Number(value);
//get the value of the profit/loss column
value1ProfitLoss = parseFloat(valueProfitLoss);
console.log ("value1ProfitLoss number first read", value1ProfitLoss);
if (valueRowNumber === 1){
sumProfitLoss = value1ProfitLoss; //make the first profitloss the seed value and place it in the database
//console.log ("sumProfitLoss first read", sumProfitLoss);
num5 = sumProfitLoss.toString();
myString1 = num5; //do silly variable transfer
myTruncatedString1 = myString1.substring(0,length1); //truncate string
//$w("#text62").text = myTruncatedString1;
///updating database with results
let b_leagueTournament = record.leagueTournament;
let b_RecommendedBet = record.recommendedBet;
let b_playerTeamOne = record.playerTeamOne;
let b_playerTeamTwo = record.playerTeamTwo;
let b_recommendedBetOdds = record.recommendedBetOdds;
let b_createdDate = record._createdDate;
let b_HFR = record.hiddenFinalResults;
let b_finalResults = record.finalResults;
let b_profitLoss = record.profitLoss;
let b_sumProfitLoss = myTruncatedString1; //new value for sumprofitloss column
let b_rowNumber = record.hiddenRowCount;
let toUpdate = {
"_id": Id,
"leagueTournament": b_leagueTournament,
"recommendedBet": b_RecommendedBet,
"playerTeamOne": b_playerTeamOne,
"playerTeamTwo": b_playerTeamTwo,
"recommendedBetOdds": b_recommendedBetOdds,
"hiddenFinalResults": b_HFR,
"_createdDate":b_createdDate,
"finalResults":b_finalResults,
"profitLoss":b_profitLoss,
"sumProfitLoss":b_sumProfitLoss,
"hiddenRowCount":b_rowNumber
};
updateBet(toUpdate);
}
if (valueRowNumber > 1 ) {
sumProfitLoss = sumProfitLoss + value1ProfitLoss; ///add the value of the next profitloss calculation and place it in db
console.log("this is sumProfitLoss ", sumProfitLoss, value1ProfitLoss, valueRowNumber);
num5 = sumProfitLoss.toString();
length1 = 5; //prepare to truncate string
myString1 = num5; //do silly variable transfer
myTruncatedString1 = myString1.substring(0,length1); //truncate string
//$w("#text62").text = myTruncatedString1;
console.log("this is my truncated string and my row number ", myTruncatedString1 + " " + valueRowNumber);
/////adding items to db since sum profit loss is now added on each row as it is made
///updating database with results
//let Id = record._id;
if (valueRowNumber === valueRowNumber1) {
let b_leagueTournament = record.leagueTournament;
let b_RecommendedBet = record.recommendedBet;
let b_playerTeamOne = record.playerTeamOne;
let b_playerTeamTwo = record.playerTeamTwo;
let b_recommendedBetOdds = record.recommendedBetOdds;
let b_createdDate = record._createdDate;
let b_HFR = record.hiddenFinalResults;
let b_finalResults = record.finalResults;
let b_profitLoss = record.profitLoss;
let b_sumProfitLoss = myTruncatedString1; //new value for sumprofitloss column
let b_rowNumber = record.hiddenRowCount;
let toUpdate = {
"_id": Id,
"leagueTournament": b_leagueTournament,
"recommendedBet": b_RecommendedBet,
"playerTeamOne": b_playerTeamOne,
"playerTeamTwo": b_playerTeamTwo,
"recommendedBetOdds": b_recommendedBetOdds,
"hiddenFinalResults": b_HFR,
"_createdDate":b_createdDate,
"finalResults":b_finalResults,
"profitLoss":b_profitLoss,
"sumProfitLoss":b_sumProfitLoss,
"hiddenRowCount":b_rowNumber
};
updateBetSum(toUpdate);
} ///end valuerownumber comparison to valuerownumber1 as experiment
}//end valuerownumber value check > 1
if (value2 === 1) {
countResults = countResults + 1;
countWinners = countWinners + 1;
//console.log("this is countResultsBottom with 1", countResults);
}
if (value2 === 0) {
countResults = countResults + 1;
//console.log("this is countResultsBottom with 0", countResults);
}
//this is the value of the total count of items that have rinal results in them
var num3 = countResults.toString();
var num15 = countWinners.toString();
var num16 = (((countWinners/num)*100).toString()).substring(0,5) +"%";
//num16 = num16 + "%";
$w("#text60").text = num3; //countResults
$w('#text76').text = num15; //countWinners
$w('#text79').text = num16; //percentage of winners
resolve('resolved');////add return to the statement
} )//end of all items loop
.catch( (err) => {
let errMsg = err.message;
let errCode = err.code;
} );
}); //results indicator end
});//end of dataset on ready function
});//// end of promise
}///end of function
//////////////////////////end of function sumprofitloss step 4///////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////