简体   繁体   English

如何检查数据是否存在?

[英]How to check if data exist?

Firebase rules configuration: Firebase规则配置:

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

Firebase database data sample Firebase数据库数据样本

在此处输入图片说明

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 那么您将可以检查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 child(path)返回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"). 相对路径可以是简单的子名称(例如,“ ada”),也可以是更深的斜杠分隔的路径(例如,“ ada / name / first”)。

https://firebase.google.com/docs/reference/js/firebase.database.Reference#child 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. 另外,孩子习惯于走一条路,所有这些孩子都处于同一水平,因此将无法正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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