【问题标题】:Firebase newData.val() not being used to check if child existsFirebase newData.val() 未用于检查孩子是否存在
【发布时间】:2020-01-05 05:33:59
【问题描述】:

在创建新用户帐户之前,我正在检查用户名是否已被使用,如果它没有将新用户名写入“用户名列表”数据库,但是当我使用数据库规则 !root.child('usernamelist').child(newData.val()).exists() 时,它没有允许我编写新数据,但如果我使用 !root.child('usernamelist').child('testuser01').exists() 它可以工作,似乎 newData.val() 出于某种原因没有使用 newData 作为子名称。

我也尝试过使用:

!root.child('usernamelist').hasChild(newData.val())
!data.hasChild(newData.val())

这是我使用的完整代码以及我的数据库和规则:

“用户名列表”数据库:

{
 "usernamelist" : {
 "testuser01" : "testuser01",
 "testuser02" : "testuser02",
 "testuser03" : "testuser03",
 "testuser04" : "testuser04",
 "testuser05" : "testuser05"
  },
}

数据库规则:

{
"rules": {

 "usernamelist": {
   ".read": "auth != false",
   ".write": "auth != false 
   && newData.exists()
   && !root.child('usernamelist').child(newData.val()).exists()",
  },

 }
}

我用来添加数据的代码:

firebase.database().ref('usernamelist').update ({
 [username]: username,
})

有谁知道问题出在哪里?

【问题讨论】:

  • 现有数据在data中可用。您是否尝试过:newData.exists() && !data.exists()

标签: javascript jquery firebase firebase-realtime-database


【解决方案1】:

我不知道为什么 .child(newData.val()) 不起作用的答案,但我想我现在已经解决了问题(见下文)。如果您找到原始问题的答案,请告诉我。

我使用的解决方法:!root.child('usernamelist/'+newData.val()).exists()

【讨论】:

    猜你喜欢
    • 2017-02-25
    • 2016-09-18
    • 2016-09-20
    • 2017-10-24
    • 2018-07-20
    • 2017-04-27
    • 1970-01-01
    • 2022-01-15
    相关资源
    最近更新 更多