Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法从angular 5中的数据库在我的页面中显示我的数组数据?_Javascript_Angular_Firebase_Firebase Realtime Database_Angular5 - Fatal编程技术网

Javascript 无法从angular 5中的数据库在我的页面中显示我的数组数据?

Javascript 无法从angular 5中的数据库在我的页面中显示我的数组数据?,javascript,angular,firebase,firebase-realtime-database,angular5,Javascript,Angular,Firebase,Firebase Realtime Database,Angular5,我希望你们都很好,所以我有一个小问题,我从firebase获取数据,我有一个包含数组的数组,我想在我的视图中显示它,但我不能,我将编写我的代码并解释我以后要实现的目标 这是我的数据库中的数据: posts --- 1 --- puslisher -- "Erick" --- content : "Something is written here" --- comments

我希望你们都很好,所以我有一个小问题,我从firebase获取数据,我有一个包含数组的数组,我想在我的视图中显示它,但我不能,我将编写我的代码并解释我以后要实现的目标

这是我的数据库中的数据:

posts 
     --- 1 
          --- puslisher -- "Erick" 
          --- content :   "Something is written here"
          --- comments 
                      --- 1
                           --- comment : "Yes"
                           --- commentator : "Patrick"  
                      --- 2 
                           --- comment : "I dont think so "
                           --- commentator : "Sarah"
     --- 2 
          --- puslisher -- "Patrick" 
          --- content :   "News here .."
          --- comments 
                      --- 1
                           --- comment : "Yes"
                           --- commentator : "Ines"  
我在app.component.ts中获取数据:

export class AppComponent implements OnInit {

    pubs:Observable<any[]>;
    constructor(private db:AngularFireDatabase){
    }
    ngOnInit(){
       this.pubs = this.getData('/posts');
    }

    getData(path):Observable<any[]>{
       return this.db.list(path).valueChanges()
    }

}
但是当我只写{{cmt}}而不是{{cmt.comment}时,我得到如下结果:

publisher: Erick
content : Something is written here 

,[object Object],[object Object]

- 
- [object Object]
- [object Object]



publisher: Patrick
content : News here 

,[object Object]

- 
- [object Object]
这意味着我正在获取我的对象,但第一个对象总是空的,我不知道为什么,我想显示我的注释文本


我希望我能很好地解释我的问题,任何帮助都将不胜感激

听起来您只是需要,这样模板就不会在定义对象之前尝试访问对象的属性

请尝试以下操作:

<li *ngFor="let cmt of post.comments" >{{ cmt?.comment }}</li>

你能检查一下cmt的未定义部分,看看它是否工作正常吗?类似cmt的东西?cmt.comment:“无数据”您可以使用json管道查看对象中的内容{{{cmt | json}}Antoine:我尝试过,我得到了正确的数据,我猜,例如,我为Patrick的注释得到了这个:{comment:是的,评论员:Patrick},您认为“”导致了问题吗?嗯,您是对的,我正在使用您的方法获取数据,但我在阵列的请求中接收到一个额外的空对象,我不知道从何处获取数据,但它是导致问题的对象,我仍然不知道如何解决该问题。谢谢您宝贵的时间,先生。很高兴我能帮忙!考虑接受和/或投票的答案,所以其他人知道我的答案是有用的箭头或你的问题已经回答了复选标记。欢迎来到SO:
publisher: Erick
content : Something is written here 

,[object Object],[object Object]

- 
- [object Object]
- [object Object]



publisher: Patrick
content : News here 

,[object Object]

- 
- [object Object]
<li *ngFor="let cmt of post.comments" >{{ cmt?.comment }}</li>