2017-01-22 43 views
2

我是Firebase的新用戶。我正在關注this Tutorial並試圖驗證數據,但不知道爲什麼我的數據未驗證。我想檢查第一個作者表,該用戶是否存在或不存在,然後他能夠寫入。用於在第二個表中寫入數據的Firebase權限用於檢查第一個表中的數據

仿真結果

Type write 
Location /posts/1/1 
Data { "Authors": { "1": true } } 
Auth { "provider": "google", "uid": "1" } 
Admin false 

寫否認

Line 69 (/posts/1/1) 
validate: "root.child('Authors').child(newData.child('Authors').val()).exists()" 

我的安全規則是:

"users": { 
    "$UID": { 
    ".read": "auth.uid == $UID", 
    ".write": "auth.uid == $UID" 
    } 
}, 
"Authors": { 
    ".read": true, 
    ".write": false 
}, 
"posts": { 
    ".read": true, 
    "$POSTID": { 
    ".write": "auth.uid != null", 
    "$UID": { 
     ".validate": "root.child('Authors').child(newData.child('Authors').val()).exists()" 
    } 
    } 
} 

回答

1

如果這就是會被寫入/posts/1/1數據:

{ 
    "Authors": { 
    "1": true 
    } 
} 

的驗證規則是爲表達newData.child('Authors').val()提取以下數據:

{ 
    "1": true 
} 

這是一個對象 - 不是字符串 - 所以它不是一個合理的價值傳遞到​​,這就是爲什麼驗證規則失敗。

這似乎有點陌生,有一個支持多個作者的結構,但如果這是你真正想要的,你可以把一個額外的驗證規則更深的層次:

"posts": { 
    ".read": true, 
    "$POSTID": { 
    ".write": "auth.uid != null", 
    "$UID": { 
     ".validate": "root.child('users').child($UID).val()).exists()", 
     "Authors": { 
     "$AUTHORID": { 
      ".validate": "root.child('Authors').child($AUTHORID).val()).exists()", 
     } 
     } 
    } 
    } 

這些規則將確保有/users入口$UID/authors入口$AUTHORID。這可能不是你想要的,但是,希望它能夠使規則本身更清楚一些。

+0

我已經改變了這些線路 ** 「寫 」: 「auth.uid!= NULL」, 「$ UID」:{ 「 .validate」:「root.child( '作者')。 ('作者')。val())。exists()「 } ** 使用此行 **」。write「:」root.child('Authors')。child(auth .uid).exists()「** 現在它工作的很完美。 –

+0

感謝您的答覆* cartant * –

相關問題