2

I want to add a new team member if team member not exists in the Firebase database. However I see a time delay while reading the existing entries. The below code returns null for the variable teammembertKey. Therefore I see a new key in database every time I re-login into the system. Can someone help me to solve this issue?

 checkIfUserExists = function (teammemberData) {
          return firebase.database().ref().child('/leaders/' + firebase.auth().currentUser.uid)
              .once("value", function (snapshot) {
                  console.log(snapshot.exists());
              return Promise.resolve({
                  teammemberData,
                  userExists: snapshot.exists(),
              });
          });
      };

      $scope.submit = function () {
          var teammembertKey = null;
          // A teammember entry.

          // A teammember entry.
          var teammemberData = {
              uid: firebase.auth().currentUser.uid,
              email: firebase.auth().currentUser.email,
              displayName: firebase.auth().currentUser.displayName,
              photoURL: firebase.auth().currentUser.photoURL
          };
          const p = checkIfUserExists(teammemberData);

          p.then((snapshot, userExists) => {
              if (userExists) {
                  teammembertKey = snapshot.key;
                  // update user
              } else {
                  // go create a user
                  console.log('i');
              }
          })
           .catch(err => {
               console.warn('Error signing in.', err);
           });

          if (teammembertKey == null) {
              // Get a key for a new team member.
              teammembertKey = firebase.auth().currentUser.uid; //firebase.database().ref().push().key;
              // Write the new member's data simultaneously.
              var updates = {};

              updates['/leaders/' + teammembertKey] = teammemberData;
              const promise = firebase.database().ref().update(updates);
              promise
                .then(e => { })
                .catch(e => {
                    console.log(e);
                })
          }

      };
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
softsara
  • 65
  • 8
  • You are dealing wrong with async callback, see https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call . Quick solution, put all your code starting from `if (teammembertKey == null)` inside `child_added` event function – Vladimir Gabrielyan Oct 27 '16 at 06:40
  • i cannot do it, because i am adding to the same node '/leaders' – softsara Oct 27 '16 at 07:22
  • 1
    okay, in this case I think it would be correct not to use `child_added` but `value` as you are just checking the value. `ref.orderByChild('email').equalTo(firebase.auth().currentUser.email).once('value', function (snap) { ... })` and then you can put your update inside callback function. – Vladimir Gabrielyan Oct 27 '16 at 07:30
  • I tried that as well. as updated above, but no luck – softsara Oct 27 '16 at 07:39

1 Answers1

0

Here is what you need.

  $scope.submit = function () {
      var teammembertKey = firebase.auth().currentUser.uid;

      var teammemberData = {
          uid: firebase.auth().currentUser.uid,
          email: firebase.auth().currentUser.email,
          displayName: firebase.auth().currentUser.displayName,
          photoURL: firebase.auth().currentUser.photoURL
      };

      firebase.database().ref().child('/leaders/' + teammembertKey).once("value")
                .then(funciton(snap) {  
                    if(!snap.val()) {
                          var updates = {};

                          updates['/leaders/' + teammembertKey] = teammemberData;
                          const promise = firebase.database().ref().update(updates);
                          promise
                            .then(e => { })
                            .catch(e => {
                                console.log(e);
                            })

                    }
                })
       .catch(err => {
           console.warn('Error signing in.', err);
       });
  };
Vladimir Gabrielyan
  • 801
  • 1
  • 11
  • 23