2017-08-04 33 views
0

你好家伙我试图把所有的注释在数组中显示在* ngFor但订阅数据来自json对象,所以它不能concat。Concat不是函数

这里是我的代码

TS

this.http.post('http://' + this.ipAdress + ':3100/api/infitineComments/:id', { "page": this.page }) 
    .map(res => res.json()) 
    .subscribe(data => { 


     if (...) { //error part 
     } 

     else { 

     let d = []; 
     for(let i = 0; i< data.length; i++){ 
      d.push(data[i]) 
     }//I did this part because data was object in array type , also 
     //doesnt work 

     this.comments = this.comments.concat(d); 

     console.log("data: ", this.comments); 
     } 
    }, err => { 
     console.log("err: ", err); 
    }); 

我的错误,这里的小图片,所有列表涵盖ID,评论,用户id ... enter image description here

谢谢

+1

你试过用String(this.comments).concat(d) –

+1

'this.comments'是否已经初始化? –

+0

@SandipPatel转换为字符串连接数组?没有意义。 –

回答

1

检查this.comments是否被很好地初始化为一个数组。

this.comments = [] 
+0

是的,它是这样初始化的 – gnncrdm

+0

但是当你初始化注释和连接它时,你确定'this'运算符是相同的吗? 你能告诉我们你如何初始化它吗? –

+0

我有点新不确定他们怎么可能是相同的,但我初始化评论:any = [];在构造函数之上,并在infitiComment()函数中与this.comments进行连接。如果我无法理解您的问题,请联系我们。 – gnncrdm