【问题标题】:Invalid argument for pipe 'AsyncPipe'管道“AsyncPipe”的参数无效
【发布时间】:2016-06-23 05:53:05
【问题描述】:

所以当我尝试从 firebase 获取数据时,我得到了这个

Invalid argument '{"-KCO4lKzEJPRq0QgkfHO":{"description":"teste","id":1457488598401,"resourceUrl":"tete","title":"test2"}}' for pipe 'AsyncPipe' in [listItems | async  in ArcListComponent@2:10]

ArcList组件

import { Component, OnInit } from "angular2/core";
import { FirebaseService } from "../shared/firebase.service";
import { ArcItem } from "./arc-item.model";


@Component({
    selector: "arc-list",
    template: `
    <ul class="arc-list">
      <li *ngFor="#item of listItems | async " class="arc-item">
        <h3>{{ item.name}}</h3><a [href]="item.resourceUrl" target="_blank" class="btn btn-success pull-right"><span>Go</span></a>
        <hr>
        <blockquote>{{ item.description }}</blockquote>
        <hr>

      </li>
    </ul>
    `

})

export class ArcListComponent implements OnInit {
  listItems: string;

  constructor(private _firebaseService: FirebaseService) {}

  ngOnInit(): any {
    this._firebaseService.getResources().subscribe(
      resource => this.listItems = JSON.stringify(resource),
      error => console.log(error)
    );
  }

}

firebase_service

import { Injectable } from "angular2/core";
import { Http } from "angular2/http";
import "rxjs/Rx";

@Injectable()
export class FirebaseService {

  constructor(private _http: Http) {}

  setResource(id: number, title: string, description: string, resourceUrl: string) {
    const body = JSON.stringify({ id: id, title: title, description: description, resourceUrl: resourceUrl});

    return this._http
                     .post("https://######.firebaseio.com/resource.json", body)
                     .map(response => response.json());
  }

  getResources() {
    return this._http
                     .get("https://######.firebaseio.com/resource.json")
                     .map(response => response.json());
  }
}

我知道我试图以错误的方式显示我的数据,但我不知道如何解决这个问题。任何帮助表示赞赏。

【问题讨论】:

    标签: javascript typescript firebase angular


    【解决方案1】:

    async 管道需要一个 observable 或一个 promise。 http.getmap 运算符返回 observable,因此您可以将返回的对象设置为组件的 listItems 属性。在这种情况下您不需要订阅:

    this.listItems = this._firebaseService.getResources();
    

    此外,这个元素将“接收”的对象必须是一个数组才能在ngFor 中使用它。您的服务从 Firebase 返回一个对象,而不是一个数组。如果要遍历对象的键,则需要实现自定义管道:

    @Pipe({name: 'keys'})
    export class KeysPipe implements PipeTransform {
      transform(value, args:string[]) : any {
        let keys = [];
        for (let key in value) {
          keys.push({key: key, value: value[key]);
        }
        return keys;
      }
    }
    

    并像这样使用它:

    @Component({
      selector: "arc-list",
      template: `
      <ul class="arc-list">
        <li *ngFor="#item of listItems | async | keys" class="arc-item">
          <h3>{{ item.value.name}}</h3><a [href]="item.value.resourceUrl" target="_blank" class="btn btn-success pull-right"><span>Go</span></a>
          <hr>
          <blockquote>{{ item.value.description }}</blockquote>
          <hr>
    
        </li>
      </ul>
      `,
      pipes: [ KeysPipe ]
    })
    

    查看这个问题了解更多详情:

    【讨论】:

    • 这是否也意味着我不需要取消订阅,因为它是由框架处理的?
    • Async 管道会自动为您处理订阅和取消订阅。
    【解决方案2】:

    async 管道与 observables 和/或 Promise 一起使用。它会为您订阅,因此您只需传递一个 observable 而无需在您的代码中订阅它:

      ngOnInit(): any {
        this.listItems = this._firebaseService.getResources()
      }
    

    【讨论】:

    • 您好,感谢您的 cmets,现在我得到了这个 Cannot find a differ supporting object '[object Object]' in [listItems | async in ArcListComponent@2:10] 请注意,我还必须将我的 listItems 类型更改为 Observable
    • @PetrosKyriakou 你的 Observable/Promise 必须返回一个数组,NgFor 不会遍历对象。
    • @EricMartinez 有你的例子吗?我对这种东西很陌生。我的意思是我明白你说的只是不知道该怎么做
    猜你喜欢
    • 1970-01-01
    • 2019-04-04
    • 2020-02-22
    • 2020-09-10
    • 2018-05-11
    • 2019-12-31
    • 2017-12-05
    • 2018-05-23
    相关资源
    最近更新 更多