child_added侦听器的时间延迟

时间:2016-10-27 05:48:38

标签: javascript firebase firebase-realtime-database

如果Firebase数据库中不存在团队成员,我想添加新的团队成员。但是,我在阅读现有条目时看到了时间延迟。以下代码为变量teammembertKey返回null。因此,每次重新登录系统时,我都会在数据库中看到一个新密钥。有人可以帮我解决这个问题吗?

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

      };

1 个答案:

答案 0 :(得分:0)

这就是你需要的。

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