-1

I have a firebase database that looks like this:

enter image description here

I want to check if a 'Data' Child Exists.

I have a start:

var check = firebase.database().ref().orderByKey().equalTo("data").once("value", function (snapshot) {
if (!snapshot.exists()) { 
    firebase.database().ref('data').set({
        messages: [],
        userIds: [],
        colours: [],
        names: [],
    });
}

});

I don't know what to put in the ref() function

CoderGuru
  • 29
  • 4
  • Have you checked [Check if value exists in Firebase db](https://stackoverflow.com/questions/37910008/check-if-value-exists-in-firebase-db) ? – Dharmaraj Feb 20 '22 at 19:58
  • I have checked it, and it doesn't really help. I have fixed my question with my work so far – CoderGuru Feb 20 '22 at 20:00
  • It's worth also mentioning that writing an empty array to the RTDB won't actually write anything because there is no data to write. An empty array is just that, empty. The RTDB will remove any empty objects from the database entirely. In addition, [arrays should be avoided](https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html). – samthecodingman Feb 20 '22 at 23:50

1 Answers1

1

It's much simpler than what you're trying:

var check = firebase.database().ref("data");
check.once("value", function (snapshot) {
  if (!snapshot.exists()) { 
    ...
  }
});
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807