gpt4 book ai didi

javascript - 迭代 Angular 4 Firebase 数据库中的对象数组

转载 作者:行者123 更新时间:2023-12-03 02:51:08 29 4
gpt4 key购买 nike

当我尝试迭代数据库时,我遇到了一些错误,我尝试了几个答案,但无济于事。下面是我的

order-details.component.html

<header class="masthead h-75 text-white text-center">
<div class="overlay"></div>
<div class="container">
<div class="home-info">
<h1 class="cover-heading">Your Order</h1>
</div>
</div>
</header>
<hr class="hidden-md-down">
<div class="container" *ngFor="let item of items">
<div class="row">
<div class="col-12">
<table class="table table-xs">
<tr>
<th></th>
<th>Description</th>
<th class="text-center">Amount</th>
<th class="text-right">Price</th>
<th class="text-right">Total</th>
</tr>
<tr class="item-row">
<td> <img [src]=item.product.imageUrl class="thumbnail img-fluid"></td>
<td>
<p> <strong>{{ item.product.title }}</strong></p>
</td>
<td class="text-right" title="Amount">{{ item.quantity}} </td>
<td class="text-right" title="Price">{{ item.product.price | currency:'NGN':true }}</td>
<td class="text-right" title="Total">{{ item.totalPrice | currency:'NGN':true }}</td>
</tr>
</table>
</div>
</div>
</div>

我的 order-details.component.ts

import { Component, OnInit } from '@angular/core';
import {AngularFireDatabase} from 'angularfire2/database';
import {AuthService} from '../auth.service';
import {OrderService} from '../order.service';
import {ActivatedRoute} from '@angular/router';

@Component({
selector: 'app-order-details',
templateUrl: './order-details.component.html',
styleUrls: ['./order-details.component.css']
})
export class OrderDetailsComponent implements OnInit {
items = {};
id;

constructor( private db: AngularFireDatabase,
public orderService: OrderService,
private auth: AuthService,
private route: ActivatedRoute) {
this.id = this.route.snapshot.paramMap.get('id');

}

ngOnInit() {
this.items = this.orderService.getOrdersByOrderId(this.id)
.subscribe(items => {
this.items = items;
console.log(this.items);
});
}
// getOrdersByOrderId() from my ORDER.SERVICE.TS
getOrdersByOrderId(orderId) {
if (!this.OrdersByOrderId$) {
this.OrdersByOrderId$ = this.db.list('/orders/' + orderId + '/items');
}
return this.OrdersByOrderId$;
}

}

我的 JavaScript 浏览器控制台出现错误。

Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays. at NgForOf.ngOnChanges (common.es5.js:1734)

下图是我的 FIREBASE 数据库树

Database Tree

最佳答案

只需在 ngOnInit() 内更改您的订阅,您将分配订阅,然后将项目分配回项目。

 ngOnInit() {
this.orderService.getOrdersByOrderId(this.id)
.subscribe(items => {
this.items = items;
console.log(this.items);
});
}

//我已经删除了这个 this.items =

关于javascript - 迭代 Angular 4 Firebase 数据库中的对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47856894/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com