Cloud Firestore可保存其他用户数据 [英] Cloud Firestore saving additional user data

查看:62
本文介绍了Cloud Firestore可保存其他用户数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Cloud Firestore数据库中保存其他用户数据?我的意思是,哪一个是正确的方法?我想创建一个用户集合,我可以在其中存储用户名和其他数据。我已经找到了关于这个问题的答案,但他们都在使用旧的Firebase实时数据库解决方案。

How can I save additional user data in Cloud Firestore database? I mean, which one is the proper way? I want to create a user collection, where I can store their username and other data. I already found answers about this problem, but all of them are using the "old" Firebase Realtime Database solution.

现在我正在这样做(这是一个Vue组件):

Now I'm doing this way (this is a Vue component):

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))
    }
  }

在上面的例子中,我只是简单的肌酸gusers集合中的新文档,并将用户的ID保存为id。这是正确的方法吗?我不确定。

In the example above, I just simply creating a new document in the "users" collection, and save the user's ID as "id". Is this the correct way? I'm not sure about it.

推荐答案

我认为你的例子没什么不好的。当然,只要您在 firestore 数据库中设置适当的安全规则:)

I think there's nothing bad in your example. Of course as long as you set up proper security rules on your firestore database :)

另一件事是IMO使用实时数据库和Firestore数据库没有区别。不同之处在于体系结构以及它如何在幕后工作,但整体用法 - 这非常相似(API与AFAIK略有不同)。

Another thing is that IMO there's no difference in using the Realtime Database and the Firestore one. The differences are in the architecture and how it works "behind the scenes", but the overall usage - this is very similar (the API is just slightly different AFAIK).

我可以向您推荐的另一种解决方案是使用Firebase函数和Firestore触发器在Web应用程序外部编写数据库 - https://firebase.google.com/docs/auth/extend-with-functions 此解决方案将删除 createUser 客户端的功能。

One alternative solution that I could recommend you is to do the database write stuff outside your web app using Firebase Functions and the Firestore Triggers - https://firebase.google.com/docs/auth/extend-with-functions This solution would remove the necessity of the createUser function on the client-side.

这篇关于Cloud Firestore可保存其他用户数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆