【问题标题】:Firebase get single itemFirebase 获取单个项目
【发布时间】:2016-10-10 04:53:09
【问题描述】:

我正在尝试创建一个页面,该页面在单个页面上显示来自 firebase 数据库的单个项目数据。


在我的 app.router.ts 我有:

{ path: 'song/:key', component: SongDetailsComponent }

在我的 song-details.component.ts 我有:

song:any;

constructor(private _route: ActivatedRoute, private _firebaseService: FirebaseService) {}

ngOnInit() {
  this._route.params.forEach((params: Params) => {
    let id = params['key'];
    this.song = this._firebaseService.getSong(id);
  });
}

在我的 song-details.componenet.html 我有:

{{ song | json }}

在我的 firebase.service.ts 我有

  songsDatabaseRef: any;

  constructor(private _angularFire: AngularFire) {
    this.songsDatabaseRef = _angularFire.database.list('songs');
  }

  getSong(key: string) {
    return this._angularFire.database.list('songs/'+key);
  }

但是当我去指定的路线查看歌曲的详细信息时,我得到一个错误

error_handler.js:45 EXCEPTION: Uncaught (in promise): 
Error: Error in ./SongDetailsComponent class SongDetailsComponent -
inline template:0:0 caused by: Converting circular structure to JSON

添加异步管道后,我得到了要显示的数据,但我无法访问 json 对象的属性。


这是 json 对象

{ "artist": "Two Friends Big Bootie Mixes", "audio": "https://api.soundcloud.com/tracks/286347553/stream?client_id=90d140308348273b897fab79f44a7c89", "dateCreated": "9/10/2016", "download": "http://smarturl.it/BB10Download", "genres": { "bass": 0, "dubstep": 30, "future": 0, "hipHop": 0, "house": 50, "indie": 0, "pop": 0, "techno": 20 }, "image": "https://i1.sndcdn.com/artworks-000186905340-xin8sf-t500x500.jpg", "title": "2F Big Bootie Mix, Volume 10 - Two Friends", "url": "http://soundcloud.com/twofriendsmixes5/bb10", "$key": "-KTgBcZudwBfhKcm68aa" }

当我尝试获取任何属性时,它说它是未定义。例如:

song title: {{(song | async | json)?.title}} 

这将返回 null!

有什么建议吗?

【问题讨论】:

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


    【解决方案1】:

    this._angularFire.database.list('songs/'+key) 是一个 Observable (FirebaseListObservable)。您需要订阅它才能获取数据。

    你可以使用:

    {{ song | async | json }}
    

    【讨论】:

    • 我有一个问题。我会更新问题
    • @MattThomas 如果您在写 {{(song | async | json)?.title}} 时考虑一下,您要求的是 json 的 title,而不是歌曲...尝试 {{ (song |async)?.title }} 如果您真的想显示它作为 json {{ (song |async)?.title |json }}.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-01
    • 2017-12-22
    • 2016-12-21
    • 2021-12-07
    • 1970-01-01
    • 2022-09-26
    • 1970-01-01
    相关资源
    最近更新 更多