代码之家  ›  专栏  ›  技术社区  ›  SuicideSheep

如何检查数据是否存在?

  •  0
  • SuicideSheep  · 技术社区  · 7 年前

    Firebase规则配置:

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

    Firebase数据库数据示例

    enter image description here

    使用此 idea ,我想到了下面的一些东西,但不起作用。

    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');
      }
    });
    

    哪一部分可能出了问题?

    1 回复  |  直到 5 年前
        1
  •  1
  •   Community Mohan Dere    5 年前

    将其更改为:

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

    然后,您将能够检查下是否存在数据 userSettings

    或者,如果要检查特定子级是否具有特定数据,则可以执行以下操作:

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

    或者如果您想检查是否有特定的孩子:

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

    也可从文件中获得:

    child

    child(path) 返回firebase。数据库参考

    获取指定相对路径处位置的引用。

    相对路径可以是一个简单的子名称(例如,“ada”)或一个更深的斜杠分隔路径(例如,“ada/name/first”)。

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

    此外,该子级用于通过一条路径,并且所有这些子级都处于同一级别,因此它不起作用。

    推荐文章