I need to recover in towerjs (with mongodb store) quote stock from all stock in all trades of one user. This code don't work, the while loop does not stop, but I do not understand why
App.Trade.where(userId: @currentUser.get('id')).order("date", "desc").all (error, trades) ->
idst = {}
i = 0
len = trades.length
while i < len
trade = trades[i]
idst[trade.get('stockTicker')] = 1 if trade.get('stockId') and not idst[trade.get('stockId')]
i++
App.Stock.where({ticker: {$in: Object.keys(idst)}}).all (error, stocks) ->
map = {}
mapQuote = {}
i = 0
len = stocks.length
while i < len
stock = stocks[i]
map[stock.get('ticker')] = stock
App.QuoteStock.where(ticker: stock.get('ticker')).order("createdAt", "desc").limit(1).first (error, quoteStock) ->
mapQuote[stock.get('ticker')] = quoteStock
i++
i = 0
len = trades.length
while i < len
trade = trades[i]
trade.stock = map[trade.get('stockTicker')]
trade.quoteStock = mapQuote[trade.get('stockTicker')]
i++
_this.trades = trades
oh my god, your code looks terrible... ( so many
i++
,i < length
, in nested scopes) please... don't write code in that way, smells too bad. nested loops leads to your code too hard to read, especially there are shadow variables.so, the problem you met is "Shadow variables", which looks like:
I suggest you improve the implementation of your algorithm, avoid using shadow variables.