pko*_*vzz 9 javascript firebase vue.js google-cloud-firestore
如何在 Cloud Firestore 数据库中保存其他用户数据?我的意思是,哪一种是正确的方法?我想创建一个用户集合,我可以在其中存储他们的用户名和其他数据。我已经找到了关于这个问题的答案,但他们都在使用“旧”的 Firebase 实时数据库解决方案。
现在我是这样做的(这是一个 Vue 组件):
methods: {
doSignUp () {
if (!this.email || !this.password) {
return
}
firebase.auth().createUserWithEmailAndPassword(this.email, this.password)
.then(data => {
this.createUser(data)
}).catch(err => console.log(err))
},
createUser (userObject) {
const docRef = this.$store.getters.getDB.collection('users')
const username = userObject.email.split('@')[0]
docRef
.doc(username)
.set({
id: userObject.uid,
username
}).then().catch((err) => console.log(err))
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,我只是简单地在“users”集合中创建一个新文档,并将用户的 ID 保存为“id”。这是正确的方法吗?我不确定。
我认为你的例子没有什么不好。当然,只要您在firestore数据库上设置了适当的安全规则:)
另一件事是,IMO 使用实时数据库和 Firestore 数据库没有区别。不同之处在于体系结构及其“幕后”的工作方式,但总体用法 - 这非常相似(API 与 AFAIK 略有不同)。
我可以向您推荐的另一种解决方案是使用 Firebase 函数和 Firestore 触发器在您的网络应用程序之外执行数据库写入操作 - https://firebase.google.com/docs/auth/extend-with-functions此解决方案将删除createUser客户端功能的必要性。
| 归档时间: |
|
| 查看次数: |
12246 次 |
| 最近记录: |