angular - 无法从 objectobservable 中获取对象名称

标签 angular firebase firebase-realtime-database angularfire observable

我遇到了一个问题,我想尝试从数据库获取一些数据。我收到数据,但无法显示它,因为它是对象而不是字符串。我怎样才能将这些对象放入字符串中我已经尝试过一些安静的事情,但似乎没有任何效果。

Component.ts

 this.artistitems = af.database.object('/users/'+this.artistid, {preserveSnapshot: true});
    this.artistitems.subscribe(snapshot => {
      this.artistvalues = snapshot.val();
      this.artistitemsid = this.artistvalues.releases;
      console.log(this.artistitemsid)
    });

artistitemsid 在控制台中的输出

aroundthefur:Object
b-sides&rarities:Object
diamondeyes(deluxe):Object
gore:Object
koinoyokan:Object
saturdaynightwrist(explicitversion):Object
thestudioalbumcollection:Object

数据库

database

我尝试放入 component.html 中的所有内容都会崩溃,我真的不知道如何从对象中获取值而不是获取对象本身

当我的 component.html 中有这个

<ul>
    <li *ngFor="let item of artistitemsid">
      {{ item.value }}
    </li>
  </ul>

我收到此错误

inline template:8:12 caused by: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.

最佳答案

如果我没记错的话,您想要显示艺术家发行的 key 列表:aroundthefur、b-sides&rarities 等。

因此,您可以从 releases 对象中提取这些 key :

this.artistitems = af.database.object('/users/'+this.artistid, {preserveSnapshot: true});
this.artistitems.subscribe(snapshot => {
  this.artistvalues = snapshot.val();
  this.artistitemsid = Object.keys(this.artistvalues.releases);
});

并使用 ngFor 迭代结果数组:

<ul>
  <li *ngFor="let item of artistitemsid">
    {{ item}}
  </li>
</ul>

关于angular - 无法从 objectobservable 中获取对象名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40526681/

相关文章:

java - 找不到参数的方法实现() [com.google.firebase :firebase-core:16. 0.1]

ios - 迭代用户以仅在 Swift 中显示他们的帖子

javascript - Firebase Realtime Database - on event - Javascript Deltasnapshot 和 Android Java DataSnapshot 的区别

javascript - 将对象数组保存到 firebase 而无需获取 0,1,2... 作为键

typescript - Angular 组件中的 "private"和 "public"

angular - Firebase 用户在登录时合并身份验证提供程序

javascript - 过渡 CSS3 在 Angular2 中不起作用?

Firebase 返回内部服务器错误

javascript - 嵌套警告——避免嵌套问题

angular - 隐藏/屏蔽辅助路由 Angular 4 的 URL