无法使用Firebase规则访问Firestore中的子集合



我正试图在Temp文档的子集合TopUp中写入,Temp集合文档有自己的写入规则,与TopUp文档不同。我希望这得到支持?

// The wildcard expression {deviceId} makes the deviceId variable available in rules.
// Store client's temporary data, use for checking serial number using Firebase Rules.
match /Temp/{deviceId} {

// Authentication is required.
allow create, update: if request.auth != null && 
// Device can only modify their own data.
request.auth.uid == deviceId;

// No reading or deletion of data is allowed in clients (mobile and web).
allow read, delete: if false;

}

// The wildcard expression {userId} makes the userId variable available in rules.
// Store client's temporary data, use for checking top up with Cloud Functions.
match /Temp/{userId}/TopUp/{docId} {

// Authentication is required.
allow create: if request.auth != null && 
// Cashiers can only top up devices that are assigned to them.
// Make sure the uid of the requesting user (cashier) matches of the device's cashier_id.
request.auth.uid == get(/databases/$(database)/documents/Devices/$(userId)).cashier_id;

// No reading, update, or deletion of data is allowed in clients (mobile and web).
allow read, update, delete: if false;

}

样本代码

firestore.collection("Temp")
.doc($('input:hidden[name=zyx]').val())
.collection("TopUp")
.doc()
.set({
cash: $("#cashField").val(),
topUp: $("#topUpField").val()
},{ merge: true }).then((doc) => {
$('#cashField').val("")
$('#topUpField').val("")
Swal.fire({
title: "Success!",
text: "Top up should reflect now on the device.",
type: "success"
}).then(function () {
hideDevicesLoading()
$("#deviceTopUpModalDetail #closeModule").click()
fetchData("deviceList", DEVICES)
});
}).catch((error) => {
showError(error.message)
hideDevicesLoading()
});

结果get()本身不包含文档的数据。data属性是包含它的对象。请尝试.data.cashier_id,如下所示:

get(/databases/$(database)/documents/Devices/$(userId)).data.cashier_id

最新更新