2016-09-29 131 views
3

我想弄清楚一切都出了问题。我所有的观察对象都返回[object Object]。AngularFire2返回[对象对象]

sunshine.component.html

<h4>Welcome {{username$}}</h4> 
<ul> 
<li *ngFor="let addsnote of addsnotes$ | async">{{ addsnote }}</li> 
</ul> 

sunshine.component.ts

import { Component, OnInit, Injectable } from '@angular/core'; 
import { FormGroup, FormControl } from '@angular/forms'; 
import { AngularFire, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2'; 

export class ProfileComponent implements OnInit { 
    private addsnotes$: FirebaseListObservable<string[]>; 
    private username$: FirebaseObjectObservable<string>; 
    private profileshow = false; 
    addForm: FormGroup; 

    constructor(private af: AngularFire){ 
    this.addForm = new FormGroup({ 
     'addnote': new FormControl() 
    }); 
    } 

    ngOnInit(){ 
    let user = firebase.auth().currentUser, 
    userNamePath = `users/${user.uid}/username`, 
    notesPath = `users/${user.uid}/addnote`; 
    this.username$ = this.af.database.object(userNamePath); 
    this.addsnotes$ = this.af.database.list(notesPath); 
    } 

    addSubmit(){this.addsnotes$.push('Billy Dee Williams');} 

} 

我的火力地堡数据库的某些部分

{ 
    "users" : { 
    "4twiyaFxVmaESXGWIfW4BwRXh893" : { 
     "addnote" : { 
     "-KSmWtpUSFXPCL4O3aKr" : "Do the dishes" 
     }, 
     "useremail" : "[email protected]", 
     "username" : "majic" 
    }, 
    "PYuSE6zyAENdKREZGTHm5VH1DXn1" : { 
     "addnote" : { 
     "-KSoZM7sH6X5ahMq7S5y" : "Du the dishes", 
     "-KSoZMimZzm6ZGQowmuL" : "Du the dishes", 
     "-KSouEXkc1UkyISGTCHd" : "Du the dishes" 
     }, 
     "useremail" : "[email protected]", 
     "username" : "asdasd" 
    } 
    } 
} 

我的网页的屏幕截图(为清楚起见)

enter image description here

编辑我已经包括了我正在进行的项目的回购这里,从而为您提供一切尽可能多的信息作为可能。希望它是有用的。

https://github.com/emjayweb/demo

各文件在SRC /应用/简档/ profile.component。由于这完全是WIP,请不要为它的后勤问题感到担忧(警卫路由,验证等)。

这种方式的作用是在主页上输入一些虚拟数据创建账户,然后点击导航上的配置文件链接。您可能必须先刷新页面。当您点击配置文件路线中的按钮时,您输入'Billy Dee Williams'到您的addnote阵列,并且视图应该反映这一点。

+2

你需要一个'| async',因为FirebaseObjectObservable也是一个需要解包的异步可见性:'

欢迎{{username $ | async}}

'。我们正在努力通过一些方法使指令更简单。 – Kato

+0

感谢您的信息。我想知道如何格式与对象的异步。这仍然不能解决我的'[对象对象]'问题。我仍然不明白为什么会发生这种情况。 – abrahamlinkedin

+2

试着将它输送到json然后看看你有什么。 {{用户名|异步| json}} – Kato

回答

2

得到了答案from a different question。我必须做的是将$.value标签添加到我的HTML。在我的名单观察到的情况下,这本来是:

<li *ngFor="let addsnote of addsnotes$ | async">{{ addsnote.$value }}</li> 

而且我观察到的对象,这本来是:

<h4>Welcome {{ (username$ | async)?.$value }}</h4> 
1

在下面的代码中,'addsnote'是一个对象数组。

<li *ngFor="let addsnote of addsnotes$ | async">{{ addsnote }}</li> 

只需检查下面的代码。

<li *ngFor="let addsnote of addsnotes$ | async">{{ addsnote[0] }}</li> 

然后您就可以了解如何访问数据。

+0

不幸的是,这最终给我空的子弹点。 – abrahamlinkedin

+0

有关其他信息,我使用代码获得的空白项目点与我的数据库中的项目数量正确对应。所以一个'li'正在打印出来。但是,这个'li'是空的。添加或删除相应的列表项会改变我在代码中的空li'标签的数量。 – abrahamlinkedin