2017-02-24 3 views
0

아래 Firebase에 json 데이터 구조가 있습니다. 새 하위 항목을 만들어야하지만 먼저 하위 항목이 있는지 확인하고 싶습니다. 그래서 기본적으로 (아이템의 배열에서) 새 아이템을 보내기 전에 ~/parentKey11/childKey21이 존재하는지 확인해야합니다.anglefire2를 사용하여 Firebase에 부모 - 자식 연결이 있는지 확인하는 방법

"nodeA": [ 
    { 
     "parentKey11": { 
     "childKey21": { 
      "items": [ 
      { 
       ... 
      }, 
      { 
       ... 
      }, 
      { 
       ... 
      } 
      ] 
     }, 
     "childKey22": { 
      "items": [ 
      { 
       ... 
      }, 
      { 
       ... 
      }, 
      { 
       ... 
      }] 
     } 
     } 
    ] 

간단히하기 위해. 먼저 부모 키를 확인할 수 있습니다. 그러나 아래 코드는 작동하지 않습니다

const parentRef = this.af.database.object(`/nodeA/parentKey11`, { preserveSnapshot: true }); 

parentRef.subscribe(data => { 
    if(data == null) { 
     console.log('data does not exists') 
    } else { 
     console.log('data exists'); 
     console.log(data); 
    } 
}); 

답변

0

이 솔루션은 나를 위해 작동 :

const parentRef = this.af.database.object(`/nodeA/parentKey11`, { preserveSnapshot: true }); 

parentRef.subscribe(data => { 
    if(data.val()==null) { 
     console.log('data does not exists') 
    } else { 
     console.log('data exists'); 
    } 
}); 



const parentChildRef = this.af.database.object(`/nodeA/parentKey11/childKey21`, { preserveSnapshot: true }); 

parentChildRef.subscribe(data => { 
    if(data.val()==null) { 
     console.log('data does not exists') 
    } else { 
     console.log('data exists'); 
    } 
});