2017-01-18 15 views
0
後火力添加數據

我的規則是這樣的 -做認證

{ 
"rules": { 
    "customer": { 
     "$user_id": { 
      ".write": false, 
      ".read" : "$user_id === auth.uid" 
     } 
    }, 
    "franchises": 
    { 
     ".read": true, 
     ".write": "root.child('customer').hasChild(newData.child('salon_id').val())" 
    } 
} 
} 

我想在加盟店這是有一個salon_id添加數據,我要確保這salon_id在客戶存在 as uid。

我收到錯誤

PERMISSION_DENIED:權限被拒絕

的代碼編輯 -

var ref = firebase.database().ref('franchises'); 
ref.push({ 
    address: address, 
    contact_no: contact_no, 
    contact_person_name: contact_person_name, 
    name: textinputPlaceName, 
    salon_id: salon_id 
}); 

數據看起來是這樣的 - enter image description here

+0

沒有看到導致此錯誤消息的最小代碼,它很難提供幫助。 –

+0

我應該發佈什麼數據結構? – Anirban

+0

當您執行某段代碼時,您包含的錯誤消息會被觸發。您需要在問題中包含觸發問題的最小代碼。 –

回答

0

你錯過了規則中的一個級別:

"franchises": { 
    ".read": true, 
    ".write": "root.child('customer').hasChild(newData.child('salon_id').val())" 
} 

這時候你寫franchises適用。但是因爲你的代碼調用了ref.push(),所以你寫了更深一層。所以規則應該是:

"franchises": { 
    ".read": true, 
    "$franchiseId": { 
     ".write": "root.child('customer').hasChild(newData.child('salon_id').val())" 
    } 
} 
+0

同樣的事情還在PERMISSION_DENIED – Anirban

+0

我只更新了規則嗎?我需要更新代碼嗎? – Anirban

+0

我將$ franchiseId作爲push()傳遞,所以它自動生成可能會導致問題。它是@frank? – Anirban