简体   繁体   中英

Ionic firestore how to check document id exist in firestore or not

I am trying to add facebook login in my app. And its successfuly working but problem is every time i login with facebook it change the firestore data. Here is my code

   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"]);

      })

  });
  }

I need to check if there is doc already available with this.fireAuth.auth.currentUser.uid so it will not add in database. IF not avaiable it will store in database.

So i need the method how can i check docID exist or not ?

There are some ways that you can get the information if a document exists on Firestore or not.

On this question of the Community here , there is all the logic for how to create a function to check a single document on the Firestore database - it's in Java, but I believe the logic should help you.

On this other post How can i get single document from Firestore with query (I'm using Ionic 4 ) , there is a query to return a single document as well, in case you think this one fits you better.

Let me know if the information helped you!

If your problem is "every time i login with facebook it change the firestore data" you can use " update " instead of " set ".

Here is the docs about it: Firestore Update Doc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM