Javascript Firebase newData.val()未用于检查子项是否存在

Javascript Firebase newData.val()未用于检查子项是否存在,javascript,jquery,firebase,firebase-realtime-database,Javascript,Jquery,Firebase,Firebase Realtime Database,在创建新用户帐户之前,我正在检查用户名是否已被使用,如果没有,则将新用户名写入“usernamelist”数据库,但在使用数据库规则时!root.child('usernamelist').child(newData.val()).exists()它不允许我写入新数据,但如果我使用!root.child('usernamelist').child('testuser01').exists()它可以工作,似乎由于某种原因,newData.val()没有将newData用作子名称 我还尝试使用: !

在创建新用户帐户之前,我正在检查用户名是否已被使用,如果没有,则将新用户名写入“usernamelist”数据库,但在使用数据库规则时
!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()",
  },

 }
}
用于添加数据的代码im:

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

有人知道问题出在哪里吗?

我不知道原因的答案。child(newData.val())不起作用,但我想我现在已经适应了一种解决方法(见下文)。如果你能找到你原来问题的答案,请告诉我


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

我不知道原因的答案。child(newData.val())不起作用,但我想我已经适应了一种解决方法(见下文)。如果你能找到你原来问题的答案,请告诉我


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

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