0

授權後,我在表「用戶」中添加用戶。此外,我還會爲此用戶添加一些值,我不希望被忽略。如何禁止重寫數據(如果存在的話)

ref.child("users").child((FIRAuth.auth()?.currentUser?.uid)!).setValue(["username": "MyName"]) 

規則

{ 
    "rules": { 
    ".read": "auth != null", 
    ".write": "auth != null", 
    "users": { 
     ".write": "!root.child('users/'+auth.uid).exists()", 
     ".read":true, 
     "positive": { 
     ".read": true, 
      ".write":false, 
     }, 
     "negative": { 
     ".read": true, 
      ".write":false, 
     }, 
    } 
    } 
} 

刪除舊數據,換上新的。

我想在服務器端寫規則,如果它已經存在,將忽略設置值。

回答

0

如果你打電話​​,您指定的屬性將被更新:

ref.child("users") 
    .child((FIRAuth.auth()?.currentUser?.uid)!) 
    .updateChildValues(["username": "MyName"]) 

Firebase documentation on updating specific fields

您的規則將不起作用,因爲您無法取消您在樹中較高級別上提供的權限。請參閱Firebase documentation on the cascading of permissions

爲了確保用戶總是具有一定的特性,使用規則的用戶的節點上:

"users": { 
    "$uid": { 
     ".write": "newData.hasChildren(['certain', 'properties])", 
+0

這是否防止被寫入到數據已經存在的節點數據? – Jay

+0

不是。這將是'!data.exists()'。 –

+0

很奇怪,我們無法控制服務器端的數據。是的,你的解決方案正在工作。不是我想要的,而是讓它成爲。 –

相關問題