我需要帮助来了解如何创建数据库系统,以便每个用户都有自己的数据,我想确保当我从我的站点发送数据时,它是用这个顺序保存的(集合名称)/(创建该数据的用户的 uid)/(名称由日期创建的文档#this 我已经完成了我只需要了解保存它的语法#)/最终数据。
有人可以帮我写他会在 js 和数据库规则中写的东西来完成我上面写的吗?
我搜索了很多教程,但我看到的例子很少,我不能总是从简单的视频中理解所有内容。
db.collection("preferenze").doc(salvataggioData).set({
animale: animale
}).then(function() {
console.log("Preferenza Salvata Con Successo")
}).catch(function(error) {
console.log(error.message)
});
setTimeout(function() {
location.reload()
}, 500);
最佳答案
如果您想使用登录用户的 uid 创建一个文档,然后将数据作为字段传递,您必须这样做:
...
auth.onAuthStateChanged(function(user) {
if (user) {
userUid = user.uid;
console.log(userUid);
console.log("Loggato")
costruzioneHtml(true);
this.user = user;
} else {
console.log("Non Loggato")
costruzioneHtml(false);
userUid = "";
}
});
...
function invioPreferenza() {
idDocumento = calcoloData();
animale = document.getElementById("txtAnimale").value;
db.collection("preferenze").doc(user.uid).set({
animale: animale,
name: user.displayName,
email: user.email,
...
}).then(function() {
console.log("Preferenza Salvata Con Successo")
}).catch(function(error) {
console.log(error.message)
});
setTimeout(function() {
location.reload()
}, 500);
}
另请查看 docs关于如何开始使用 Firebase 身份验证的 Firebase 指南,以获取有关如何使用来自 auth()
的信息的更多信息。
编辑
我认为您的安全规则应该如下所示,但我不是 100% 确定:
service cloud.firestore {
match /databases/{database}/documents {
match /users/{userID} {
allow read, write: if request.auth.uid == userID;
}
}
}
关于javascript - FIrebase:使用用户 UID 创建一个数据库,并使其仅在登录且 UID 相同时才可访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57753090/