【问题标题】:Ionic firestore how to check document id exist in firestore or not离子火库如何检查火库中是否存在文档ID
【发布时间】:2020-05-28 03:00:36
【问题描述】:

我正在尝试在我的应用中添加 facebook 登录。它成功地工作,但问题是每次我用 facebook 登录它都会改变 firestore 数据。这是我的代码

   onLoginSuccess(res: FacebookLoginResponse) {
    const credential = firebase.auth.FacebookAuthProvider.credential(res.authResponse.accessToken);
    this.fireAuth.auth.signInWithCredential(credential)
      .then((response) => {
      console.log(response);

        this.fb.api('me?fields=id,name,email,first_name,picture.width(720).height(720).as(picture_large)', []).then(profile => {
        this.userData = {email: profile['email'], first_name: profile['first_name'], picture: profile['picture_large']['data']['url'], username: profile['name']}
        console.log(this.userData);
        console.log(this.fireAuth.auth.currentUser.uid);

          let userData =  {
          email: this.userData.email,
          username: this.userData.username,
          photo: this.userData.picture,
          phone: '',
          point : '0',
          uID: this.fireAuth.auth.currentUser.uid
        };

        this.angularFirestore.collection('AppUsers').doc(this.fireAuth.auth.currentUser.uid).set(userData); //Here is the problem
        this.router.navigate(["/select"]);

      })

  });
  }

我需要检查this.fireAuth.auth.currentUser.uid 是否已经有可用的文档,所以它不会添加到数据库中。如果不可用,它将存储在数据库中。

所以我需要如何检查 docID 是否存在的方法?

【问题讨论】:

  • 检查文档是否存在之前已经问过:stackoverflow.com/…。你有没有尝试过类似的东西?

标签: javascript angular ionic-framework google-cloud-firestore


【解决方案1】:

如果您的问题是“每次我使用 facebook 登录都会更改 firestore 数据”,您可以使用“update”而不是“set”。

这里是关于它的文档:Firestore Update Doc

【讨论】:

  • 但是在第一次登录时我需要为第一个用户设置一些值
【解决方案2】:

有一些方法可以让您在 Firestore 上是否存在文档时获取信息。

关于社区here 的这个问题,有关于如何创建一个函数来检查 Firestore 数据库中的单个文档的所有逻辑 - 它是用 Java 编写的,但我相信这些逻辑应该对您有所帮助。

在另一篇帖子How can i get single document from Firestore with query (I'm using Ionic 4 ) 上,还有一个返回单个文档的查询,以防您认为这个更适合您。

如果这些信息对您有帮助,请告诉我!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-01
    • 2020-04-24
    • 2016-09-10
    • 2019-06-01
    • 2019-01-09
    • 1970-01-01
    • 1970-01-01
    • 2010-10-22
    相关资源
    最近更新 更多