【发布时间】:2021-01-29 10:19:23
【问题描述】:
我有一个 firebase 项目,自上次构建以来,它不会创建新文档。它将保存和修改文档,但不会创建新文档。
创建新文档的函数应该使用基于新创建的 uid 的键创建一个新文档,如下所示:
const sendInformationHandler = () => {
const customerEmailCurrent = customerEmail.current.value
const customerPassword = "password"
if (checkUserEmailExists(customerEmailCurrent)) {
createUser(
{
email: customerEmailCurrent,
password: customerPassword
}
).then(function(user) {
firebase
.firestore()
.collection('sessions')
.doc(user.data.uid).set(
{...context.state}
).then(() => {
console.log("DATA SAVED SUCCESSFULLY!")
}).then(() => {
dispatch({ type: refreshContextFromCache, payload: INITIAL_CONTEXT})
}).then(() => {
push('/');
})
}).catch(err => console.log("AN ERROR OCCURRED", err))
} else {
console.log("EMAIL ALREADY EXISTS!!!!!!")
}
};
我将 checkUserEmailExists 设置为始终返回 true。 createUser 是云中的一个 firebase 函数,用于创建新用户。当这个函数被触发时,它正在重定向,然后出错。
错误说是权限错误,但我的 firebase api 密钥没有更改,我可以修改现有记录。
我的数据库规则也没有改变并且基于令牌,但令牌仍然有效:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth.token.admin == true;
}
match /{document=**} {
allow create: if request.auth.token.coach == true;
}
match /sessions/{userId} {
allow read, write: if request.auth.token.customer == true && request.auth.uid == userId;
}
}
}
在本地,在我推送之前,我在 firebase cli 中为正确的项目调用了 firebase use,并在本地也使用 gcp 切换到了正确的项目。
我的 firebaserc 是这样的:
{
"targets": {
"prod": {
"hosting": {
"prod": [
"prod-project"
]
}
},
"develop": {
"hosting": {
"prod": [
"prod-project"
]
}
},
"prod-project": {
"hosting": {
"prod": [
"prod-project"
]
}
},
"ammonite-testing": {
"hosting": {
"prod": [
"prod-project"
]
}
}
},
"projects": {
"prod": "prod-project",
"default": "prod-project"
}
}
我的 firebase.json 是这样的:
{
"functions": {
"source": "functions"
},
"hosting": {
"target": "prod",
"public": "build",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
}
}
发生了什么事?如果firebase权限不起作用?为什么我还能保存?为什么它在这个最新的 ci 版本上坏了,而不是以前?
【问题讨论】:
-
您的规则中有很多overlapping,尤其是重叠,这意味着不同的自定义声明。您应该在此级别进行调试,尝试查看哪个规则阻止创建文档。
-
规则一直都是这样的,以前也有效吗?
-
好的,这是因为规则将它们设置为允许所有修复它。但是我仍然不明白为什么一直以来这些确切的规则都很好?
标签: javascript firebase google-cloud-firestore firebase-authentication