return 词典 flask 和 angular

return a dictionary of dictionaries flask and angular

我正在使用 flask 和 ionic(angular) 创建一个应用程序,我正在尝试 return 列表中的 JSON 目前我的 python 代码如下

def get-stocks():
# Select all stocks 
cursor.execute("""SELECT * FROM `tbl_symbol_index`""")
stocks = cursor.fetchall()
stockList = []
data = {}
for stock in stocks:
    symbol = stock[0]
    companyName = stock[1]
    stockList.append({
        "symbol": symbol,
        "companyName": companyName
    })
data = {'stocks': stockList}
print(len(data["stocks"]))
return jsonify(data)

这 return 是以下词典。

data = {"stocks": [
         "Symbol": "TSLA",
         "companyName": "Tesla Inc"
          ],
          [
         "Symbol": "MSFT",
         "companyName": "Microsoft"
          ],
          [
         "Symbol": "AAPL",
         "companyName": "Apple"
          ],
}

我使用

从我的 angular 调用该函数
ngOnInit() {
// Get a list of all existing stocks
const Stocks = this.initializeItems();

}

  async initializeItems(){
    this.http.get("http://127.0.0.1:5000/get-stocks").subscribe(function(data) {
    })
  }

但出于某种原因我无法在 HTML

中循环遍历它
<ion-list>
  <ion-item *ngFor="let data of Stocks | filter:filterTerm">
    <ion-label>{{data.symbol}}</ion-label>
    <ion-label>{{data.companyName}}</ion-label>
  </ion-item>
</ion-list>

你能试试这个吗:

@Component(...)
export class Component {
 data;
 ngOnInit() {
  // Get a list of all existing stocks
  this.initializeItems();
 }
 initializeItems(){
    this.http.get("http://127.0.0.1:5000/get-stocks").subscribe(datas => {
       this.data = datas;
        console.log(this.data.stocks)
    });
  }
}