Joseph Chikeme
Joseph Chikeme

Reputation: 512

Firebase Firestore custom ID not creating new document after creating user properly

I created a sample registration form in my Vue App to automatically create a Firestore document with the User UID attached to it with a custom document ID.

The user gets created successfully but the document doesn't get created and doesn't display any error on the console even after using the catch() error method.

register() {
  //Generate pin
  function generateQuickGuid() {
    return Math.random()
      .toString(36)
      .substring(2, 15);
  }
  let ref = fs.doc(generateQuickGuid());
  ref.get().then(doc => {
    if (doc.exists) {
      console.log("Pin Exists");
    } else {
      console.log("pin doesnt exists");
      // then add user to firestore
      if (
        this.email &&
        this.password &&
        this.displayName &&
        this.category
      ) {
        auth
          .createUserWithEmailAndPassword(this.email, this.password)
          .then(cred => {
            ref
              .set({
                Name: this.displayName,
                address: "close to understanding, firebase, auth",
                phone: "09808763987",
                category: this.category,
                alias: pin,
                user_id: cred.user.uid
              })
              .catch(e => {
                console.log(e);
              });

            console.log("User Added");
          })
          .catch(error => {
            // Handle Errors here.
            var errorCode = error.code;
            var errorMessage = error.message;
            // ...
            console.log(errorCode, errorMessage);
          });
      }
    }
  });
}

Upvotes: 1

Views: 519

Answers (1)

Niteesh
Niteesh

Reputation: 3130

register() {
  //Generate pin
  function generateQuickGuid() {
    return Math.random()
      .toString(36)
      .substring(2, 15);
  }
  let ref = fs.doc(generateQuickGuid());
  ref.get().then(doc => {
    if (doc.exists) {
      console.log("Pin Exists");
    } else {
      console.log("pin doesnt exists");
      // then add user to firestore
      if (
        this.email &&
        this.password &&
        this.displayName &&
        this.category
      ) {
        AuthResult authResult = auth
          .createUserWithEmailAndPassword(this.email, this.password)
          .catch(error => {
            // Handle Errors here.
            var errorCode = error.code;
            var errorMessage = error.message;
            // ...
            console.log(errorCode, errorMessage);
          });

       ref.set({
                Name: this.displayName,
                address: "close to understanding, firebase, auth",
                phone: "09808763987",
                category: this.category,
                alias: pin,
                user_id: authResult.getUser().getUid()
              })
              .catch(e => {
                console.log(e);
              });
      }
    }
  });
}

Try this

Upvotes: 1

Related Questions