我在网络应用程序中将代码更新为 Firebase 的
10.5.0
版本(我正在使用从 here 获得的替代方法),但我仍然面临权限错误,即使我的代码之前工作正常且没有权限错误我尝试升级到 10.5.0
(我正在使用 8.2.0
)。
这是我的 upload.js
文件:
import {initializeApp} from 'https://www.gstatic.com/firebasejs/10.5.0/firebase-app.js';
import {
getStorage,
ref as storageRef,
uploadBytesResumable,
getDownloadURL,
} from 'https://www.gstatic.com/firebasejs/10.5.0/firebase-storage.js';
import {getAuth, onAuthStateChanged, signOut} from 'https://www.gstatic.com/firebasejs/10.5.0/firebase-auth.js';
import {getDatabase, ref as databaseRef, push, set} from 'https://www.gstatic.com/firebasejs/10.5.0/firebase-database.js';
// all data below is correctly put
const app = initializeApp({
apiKey: '',
authDomain: '',
databaseURL: '',
projectId: '',
storageBucket: '',
messagingSenderId: '',
});
const auth = getAuth(app);
const database = getDatabase(app);
const storage = getStorage(app);
$('#addNew').on('submit', function (e) {
e.preventDefault();
var file = $('#pic').prop('files')[0];
const fileExt = $('#pic').prop('files')[0].name.split('.').pop();
const fileName = `${getRandomString(15)}.${fileExt}`;
var listStorageRef = storageRef(storage, `list/images/${fileName}`);
var uploadTask = uploadBytesResumable(listStorageRef, file);
uploadTask.on(
'state_changed',
function (snapshot) {
// progress being handled here
},
function (error) {
console.log(error); // doesn't log anything
showMessageByID('Unknown error occurred.', '#message');
},
function () {
getDownloadURL(listStorageRef).then(function (downloadURL) {
const listRef = databaseRef(database, 'list');
const newRef = push(listRef);
console.log(downloadURL);
set(listRef, {
name,
// there are other data here
})
.then(function () {
const id = newRef.key;
// some other operations here
})
.catch(function (error) {
console.log('Error object:', error); // for some reason this is not logging the permission error
showMessageByID(error.message, '#message'); // this is logging
});
});
},
);
});
我像这样导入
upload.js
:
<script type="module" src="js/firebase/upload.js"></script>
数据库安全规则:
"list": {
"$id": {
".write": "newData.child('createdBy').val() == auth.uid",
".read": false
},
".indexOn": "createdBy"
},
您正在数据库中的
set
节点上调用 list
,但您的规则不提供任何写入该节点的权限。从您共享的规则来看,您似乎希望允许在 list
下添加新的子节点,在这种情况下,您需要在 set
而不是 newRef
上调用 listRef
。