将具有角度的Firebase可观察映射到模板

时间:2018-02-14 04:06:00

标签: javascript angular firebase firebase-realtime-database observable

我没有使用Angular迭代Firebase observable以仅接收用户订单历史记录并将其输出到模板。我已经将switchMap更改为map以消除错误,但我似乎无法像以前那样从模板访问数据......我是新手。请在下面找到所有必要的文件和数据库结构。enter image description here

此代码......

 this.orders$ = authService.user$.switchMap(u => orderService.getOrdersByUser(u.uid));

...给我这个错误......

Argument of type '(u: User) => Query' is not assignable to parameter of type '(value: User, index: number) => ObservableInput<{}>'.
  Type 'Query' is not assignable to type 'ObservableInput<{}>'.
    Type 'Query' is not assignable to type 'ArrayLike<{}>'.
      Property 'length' is missing in type 'Query'.

......我完全失败了。如果您有任何建议,我将非常感激。谢谢!

这是我的组件文件:

import { AuthService } from './../auth.service';
import { OrderService } from './../order.service';
import { Component, OnInit } from '@angular/core';
import 'rxjs/add/operator/switchMap';

@Component({
    selector: 'app-my-orders',
    templateUrl: './my-orders.component.html',
    styleUrls: ['./my-orders.component.css']
})
export class MyOrdersComponent {
    orders$;

    constructor(
        private authService: AuthService,
        private orderService: OrderService) {

        this.orders$ = authService.user$.switchMap(u => orderService.getOrdersByUser(u.uid));
        console.log(this.orders$);
    }
}

服务文件:

import { AngularFireDatabase } from 'angularfire2/database-deprecated';
import { Injectable } from '@angular/core';
import { CartService } from './cart.service';

@Injectable()
export class OrderService {
    constructor(
        private db: AngularFireDatabase,
        private cartService: CartService
    ) { }
    getOrdersByUser(userId: string) {
        return this.db.list('/orders').$ref.orderByChild('userId').equalTo(userId);
    }

}

和MARKUP:

<h1>Orders</h1>
<table class="table">
    <thead>
        <tr>
            <th>Customer</th>
            <th>Date</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        <tr *ngFor="let order of orders$ | async">
            <td>{{ order.shipping.name }}</td>
            <td>{{ order.datePlaced | date}}</td>
            <td>
                <a href="#">View</a>
            </td>
        </tr>
    </tbody>
</table>

enter image description here

3 个答案:

答案 0 :(得分:2)

查询只能通过一种方法排序。这意味着您只能指定orderByChild,orderByKey,orderByPriority或orderByValue。

如果您想使用动态查询:https://github.com/angular/angularfire2/blob/master/docs/rtdb/querying-lists.md

从服务文件中删除equalTo查询。

服务文件:

 import { AngularFireDatabase } from 'angularfire2/database-deprecated';
 import { Injectable } from '@angular/core';
 import { CartService } from './cart.service';

  @Injectable()
   export class OrderService {
    constructor(
     private db: AngularFireDatabase,
     private cartService: CartService
 ) { }
   getOrdersByUser(userId: string) {
    return 
 this.db.list('/orders').$ref.orderByChild('userId');
}

}

答案 1 :(得分:0)

我现在尝试了几件事......

组件文件:     命令;  this.orders = authService.user $ .map(u =&gt; orderService.getOrdersByUser(u.uid))。subscribe();         的console.log(this.orders); 标记: {{订单}}

浏览器结果: [object Object]

和..

模板: {{orders $ |异步}

浏览器结果: https://shopdb432.firebaseio.com/orders

答案 2 :(得分:0)

    getOrdersByUser(userId: string) {
    return this.db.list('/orders', ref =>
    ref.orderByChild('userId').equalTo(userId)).valueChanges();
  }