为什么我不能在FirebaseListObservable上调用toArray()? [英] Why can I not call toArray() on FirebaseListObservable?

查看:210
本文介绍了为什么我不能在FirebaseListObservable上调用toArray()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个Ionic Project中,我有:
$ b $ p $ import $ {angularfire2 / database'中的{AngularFireDatabase,FirebaseListObservable}; $ b

以及包含字段的类:

歌曲:FirebaseListObservable< any> ;


$ b

因此,这行代码:
$ b $ <$> c> this.songs = db.list('/ songs');



工作原理并允许我放行:



<按钮离子按钮ouline * ngFor =让歌曲歌曲|异步>



在我的html没有问题。

现在, FirebaseListObservable 扩展RxJS Observable )。
此外, Observable 有一个方法 toArray() 。但是,当我运行我的项目,我看到:

pre $ Typescript错误
属性'toArray'不存在类型'FirebaseListObservable<任何>'。

这是为什么?有没有另外一种方法,我可以从歌曲正在观察?

解决方案

我不确定为什么 toArray()不起作用,但是我可以建议你从数据库中获得你想要的数组。这样做,当我想只是阵列没有能力听任何数据库的变化 - 就像Observable一样):

pre $ .db.list('/ songs')
.first()。toPromise()
.then(response => {
//处理响应的代码 - 一个列表

This.items =响应;
})
.catch(错误=> {/ /错误代码在这里));

不要忘记先导入 rxjs toPromise



我真的希望它符合你的愿望,并帮助你:)

In an Ionic Project, I have:

import { AngularFireDatabase, FirebaseListObservable} from 'angularfire2/database';

and a class with the field:

songs: FirebaseListObservable<any>;

therefore, the line of code:

this.songs = db.list('/songs');

works and allows me to put the line:

<button ion-button ouline *ngFor="let song of songs | async">

in my html without problem.

Now, FirebaseListObservable extends the RxJS Observable (source). Furthermore, Observable has a method toArray(). But, when I run my project, I see:

Typescript Error
Property 'toArray' does not exist on type 'FirebaseListObservable<any>'.

Why is this? Is there another way I can get an array from what songs is observing?

解决方案

I don't really sure why the toArray() not working , but i can suggest you a way to get the array you want from the DB.( I usually do that when i want just the array without the ability to listen to any changes of the DB - like Observable does) :

this.db.list('/songs')
.first().toPromise()
.then(response => {
  //code to handle the response - in this case its a list

  This.items = response;
})
.catch(error => { //error code here });

dont forget to import the rxjs first and toPromise

I really hope it fits your wish and helps you :)

这篇关于为什么我不能在FirebaseListObservable上调用toArray()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆