0

I had someone beta-test the website today and they ran into an internal server error when trying to return to the index/show portfolio of stocks screen. The error was this:

 File "/home/ubuntu/pset8/finance/application.py", line 68, in index stock_info["total"] = stock_info["shares"] * stock_info["price"] TypeError: unsupported operand type(s) for *: 'NoneType' and 'float' 

This is the section of the code I think the problem is in, but when querying the database, I'm getting (correct) values.

 if stock_symbols != []: stocks = [] for symbol in stock_symbols: symbol_data = lookup(symbol["symbol"]) stock_shares = db.execute("SELECT SUM(shares) FROM portfolio WHERE user_id=:user_id AND symbol=:symbol;", user_id=session["user_id"], symbol=symbol_data["symbol"]) if stock_shares[0]["SUM(shares)"] == 0: continue else: stock_info = {} stock_info["name"] = symbol_data["name"] stock_info["symbol"] = symbol_data["symbol"] stock_info["price"] = symbol_data["price"] stock_info["shares"] = stock_shares[0]["SUM(shares)"] stock_info["total"] = stock_info["shares"] * stock_info["price"] stocks.append(stock_info) 

I see that it says stock_info["shares"] (and therefore stock_shares[0]["SUM(shares)"], but again, when querying for this, I am getting real values) is the NoneType, but where does that happen? Strangely enough, this doesn't seem to be a problem for all users. Some of my users can access their index/portfolio page with no problems, whereas others run into this error every time, even upon login.

Thoughts?

1 Answer 1

0

If this SELECT SUM(shares) FROM portfolio WHERE user_id=:user_id AND symbol=:symbol;" finds no matching rows, it will return one row with None for SUM(shares).

sqlite has an aggregate function total() that will return 0.00 instead of NULL.

1
  • This seemed to solve it, although now I have a new problem - purchases are appearing in the portfolio twice! Undo one knot, find another. Commented Feb 28, 2020 at 1:02

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.