简体   繁体   中英

How to check if data exist?

Firebase rules configuration:

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

Firebase database data sample

在此处输入图片说明

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM