client.on('salelog', function(data) { console.log(data); pool.query("SELECT (user, skin, wear, pattern, tier, listprice, profit, listing, inspect, sniper, date) FROM UserPuchases WHERE listing ='" + data.snipelink "'", function(err, result) { if (err) throw err; var snipedata = JSON.parse(JSON.stringify(result)); var snipedatalength = snipedata.length var user = snipedata[0].user; var buyprice = parseFloat((snipedata[0].listprice).replace('$', '')); var soldprice = parseFloat((data.saleprice).replace('$', '')); var profit = "$" + (soldprice - buyprice); sellprice = "$" + sellprice; var ProfitData = new ProfitConstructor(user, buyprice, soldprice, profit, snipelink, salelink); pool.query("UPDATE UserPurchases SET sellprice = '" + data.saleprice + "', salelink = '" + data.salelink + "', profit = '" + profit + "' WHERE listing = '" + data.snipelink + "'", function(err, result) { if (err) throw err; io.emit('saleupdated', ProfitData); pool.query("SELECT * FROM UserPurchases", function(err, result) { if (err) throw err; pupdated = JSON.parse(JSON.stringify(result)); var puplength = pupdated.length; var Total = new Totals(cost, sales, profit); var SoldTotal = new SoldTotals(soldcost, soldsales, soldprofit); var DogeTotal = new DogeTotals(dogecost, dogesales, dogeprofit); var NawtyTotal = new NawtyTotals(nawtycost, nawtysales, nawtyprofit); var totals = function tt () { var for (var x = 0; x < puplength; x++) { var profit = pupdated[x].profit; var cost = var totalprofit + profit; if (x === puplength - 1) { return totalprofit; } } } }); }); }); }); }); function ProfitConstructor(user, buyprice, soldprice, profit, snipelink, salelink, profittotal) { this.user = user; this.buyprice = buyprice; this.soldprice = soldprice; this.profit = profit; this.snipelink = snipelink; this.salelink = salelink; } function Totals(cost, sales, profit) { this.cost = cost; this.sales = sales; this.profit = profit; } function SoldTotals(soldcost, soldsales, soldprofit) { this.soldcost = soldcost; this.soldsales = soldsales; this.soldprofit = soldprofit; } function DogeTotals(dogecost, dogesales, dogeprofit) { this.dogecost = dogecost; this.dogesales = dogesales; this.dogeprofit = dogeprofit; } function NawtyTotals(nawtycost, nawtysales, nawtyprofit) { this.dogecost = nawtycost; this.dogesales = nawtysales; this.dogeprofit = nawtyprofit; } function AllTotals(Total, SoldTotal, DogeTotal, NawtyTotal) { this.total = Total; this.soldtotal = SoldTotal; this.dogetotal = DogeTotal; this.nawtytotal = NawtyTotal; }