0

Firebase rules configuration:

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null"
  }
}

Firebase database data sample

enter image description here

Using this idea, I came up with something as below but not working.

const { currentUser } = firebase.auth();

console.log(currentUser.uid);
  
firebase.database().
ref(`/users/${currentUser.uid}/userSettings`)
.child({ userSetting_DisplayName, userSetting_City, userSetting_DailyLimit })
.once('value', function(snapshot) {
  if (snapshot.exists()) {
    console.log('exist');
  }else{
    console.log('not exist');
  }
});

which part could've gone wrong?

Community
  • 1
  • 1
SuicideSheep
  • 5,260
  • 19
  • 64
  • 117
  • I think this line: _`.child({ userSetting_DisplayName, userSetting_City, userSetting_DailyLimit })`_ because i doubt you can use an object as a child – André Kool Mar 23 '18 at 08:32
  • Here https://stackoverflow.com/a/39236689/3332734 and https://stackoverflow.com/a/44852086/3332734 – Francis Rodrigues Mar 28 '18 at 22:29

1 Answers1

1

Change it to this:

firebase.database().
ref(`/users/${currentUser.uid}/userSettings`)
.once('value', function(snapshot) {
if (snapshot.exists()) {
console.log('exist');
}else{
console.log('not exist');
  }
});

then you will be able to check if data exists under userSettings

or if you want to check if a particular child has a certain data, then you can do this:

ref(`/users/${currentUser.uid}/userSettings`).orderByChild("userSetting_city").equalTo(valuehere).once(...

or if you want check if a particular child is there:

ref(`/users/${currentUser.uid}/userSettings`).child("userSetting_DisplayName").once(...

Also from the docs:

child

child(path) returns firebase.database.Reference

Gets a Reference for the location at the specified relative path.

The relative path can either be a simple child name (for example, "ada") or a deeper slash-separated path (for example, "ada/name/first").

https://firebase.google.com/docs/reference/js/firebase.database.Reference#child

Also the child is used to go through a path and all these children are in the same level, so it won't work.

Community
  • 1
  • 1
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134