简体   繁体   English

以角度将 Firebase observable 映射到模板

[英]Mapping an Firebase observable in angular to the template

I am failing to use Angular to iterate over a Firebase observable to receive only the users order history and output it to the template.我无法使用 Angular 迭代 Firebase observable 以仅接收用户订单历史记录并将其输出到模板。 I've gotten as far as changing switchMap to map which gets rid of the error but then I can't seem to access the data from the template like I am used to... I am new to this.我已经将 switchMap 更改为 map 以消除错误,但是我似乎无法像以前那样访问模板中的数据......我对此很陌生。 Please find all necessary files and database structure below.请在下面找到所有必要的文件和数据库结构。 在此处输入图片说明

This code...这段代码...

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

...is giving me this error... ......给我这个错误......

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'.

... and I am at a total loss. ......我完全不知所措。 If you have any suggestions I would be very appreciative.如果您有任何建议,我将不胜感激。 Thank you!谢谢!

Here is my COMPONENT FILE:这是我的组件文件:

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$);
    }
}

SERVICE FILE:服务文件:

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);
    }

}

and 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>

在此处输入图片说明

Queries can only be ordered by one method.查询只能通过一种方法进行排序。 This means you can only specify orderByChild, orderByKey, orderByPriority, or orderByValue.这意味着您只能指定 orderByChild、orderByKey、orderByPriority 或 orderByValue。

if you want to use dynamic query: https://github.com/angular/angularfire2/blob/master/docs/rtdb/querying-lists.md如果你想使用动态查询: https : //github.com/angular/angularfire2/blob/master/docs/rtdb/querying-lists.md

Remove the equalTo query from your service file.从您的服务文件中删除 equalTo 查询。

SERVICE FILE:服务文件:

 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');
}

} }

I've tried a couple things now...我现在尝试了几件事......

component file: orders;组件文件:订单; this.orders = authService.user$.map(u => orderService.getOrdersByUser(u.uid)).subscribe(); this.orders = authService.user$.map(u => orderService.getOrdersByUser(u.uid)).subscribe(); console.log(this.orders);控制台.log(this.orders); markup: {{orders}}标记:{{订单}}

browser result: [object Object]浏览器结果:[object Object]

And..还有..

template: {{orders$ |模板:{{订单$ | async}异步}

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ObjectUnsubscribedErrorImpl 在 Angular 中模板中的 observable 上 - ObjectUnsubscribedErrorImpl on an observable in a template in Angular Angular Firebase 集成在模板中 - Angular Firebase integration in template Angular 4 Firebase操作* ngFor Observable或在Observable中使用索引 - Angular 4 Firebase manipulate *ngFor of Observable or use index in an Observable Angular Firebase 预期验证器返回 Promise 或 Observable - Angular Firebase Expected validator to return Promise or Observable Angular 4/5可观察-如何根据可观察属性更新组件模板? - Angular 4/5 Observable - How to update component template depending on observable property? Angular2模板中未呈现可观察事件 - Observable event not rendered in Angular2 template Angular 5:可观察链中的更新模板不起作用 - Angular 5: Updating template in Observable chain not working 使用 Angular 模板中的 Observable 的值初始化 BehaviourSubject - Initialize BehaviourSubject with value from an Observable in an Angular template Angular 显示模板,如果 observable 是错误的异步 pipe - Angular display template if observable is falsey with async pipe Angular 模板未在 RxJS 上更新 可观察到的变化 - Angular template not updating on RxJS Observable change
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM