2017-12-20 6 views
1

firebase에서받는 간단한 데이터가 있지만 해당 데이터에서 필터가 작동하지 않습니다. 많은 게시물을 검색했지만 도움이되지 않습니다. 누락 된 항목이 무엇입니까? 모든 도움을필터가 각도로 작동하지 않습니까?

나는 그것의 내부 OnInit() 경우 같은 데이터를 얻을 수 있습니다.

export class SignUpComponent implements OnInit { 
ngOnInit() { 
const someEvemnts = [ 
{type: 'attack', value: 1, target: 'dorkman'}, 
{type: 'yaeed', value: 2, target: 'dorkman'}, 
{type: 'attack', value: 3, target: 'fluffy'}, 
{type: 'attack', value: 4, target: 'dorkman'}, 
] 
const total = someEvemnts 
.filter(event => event.type== 'attack') 
.filter(event => event.target== 'dorkman') 
console.log(total); // works fine 

let check = this.values; // works fine 
let check2 = this.values.filter(val => val) // not works 
console.log(check); 
console.log(check2); // returns empty 
this.SignIn(); 
} 

SignIn(){ 
    firebase.database().ref('/companies').on('child_added', (data)=>{ 
     this.values.push(data.val()) 
    } 
) 
} 
values=[]; 
} 

콘솔 :

console 은 아래에있는 내 코드입니다.

답변

0
let check2 = this.values.filter(val => val) // not works 

당신이

let check2 = this.values.filter(val => val.isAwesome) 

무엇을 필터링 할 않는 같은 표현을 해달라고하면 그것은 작동하지 않을 수 있습니까? 문자열을 필터링하려면 다음과 같이하십시오.

let check2 = this.values.filter(val => val.label === 'hello') 
+0

이 acheive하려고하지만 –

+1

변수 네가 원하는 필터를 expectd이로 그 작동하지 이잖아? 데이터에 부울이 없습니다. 귀하의 코드는 필터링 할 수없는 전체 객체를 필터링하므로 아무 것도 반환하지 않습니다. – CodeNashor

+0

let check2 = this.values.filter (val => val.name == 'shanth') –

1

this.SignIn();을 호출 했으므로 작동하지 않습니다. console.log를 호출 한 후 "Values"변수를 채 웁니다. 따라서 콘솔을 할 때 "값"은 비어 있습니다.

변수가 많이 사용 된 후에 "console.log"를 실행하십시오.

이 시도 :

export class SignUpComponent implements OnInit { 
ngOnInit() { 
const someEvemnts = [ 
{type: 'attack', value: 1, target: 'dorkman'}, 
{type: 'yaeed', value: 2, target: 'dorkman'}, 
{type: 'attack', value: 3, target: 'fluffy'}, 
{type: 'attack', value: 4, target: 'dorkman'}, 
] 
const total = someEvemnts 
.filter(event => event.type== 'attack') 
.filter(event => event.target== 'dorkman') 
console.log(total); // works fine 

this.SignIn(); 
} 

SignIn(){ 
    firebase.database().ref('/companies').on('child_added', (data)=>{ 
     this.values.push(data.val()) 

     let check = this.values; // works fine 
     let check2 = this.values.filter(val => val) // not works 
     console.log(check); 
     console.log(check2); // returns empty 
    } 
) 
} 
values=[]; 
} 
+0

데이터베이스의 응답에 수정하거나 html 이벤트로 필터링하여 다른 기능을 트리거 할 수 있습니다. – Swoox

+0

하지만 먼저 데이터를 값으로 푸시하고 싶습니다. 그런 다음 해당 데이터에 액세스하려고합니다. –