StartAt and endAt doesn't work as expected using corner fire: 0.8.0

Using AngularFire 8.0

Below is the code that uses startAt and endAt to limit data from firebase.

$rootScope.$on('$firebaseSimpleLogin:login', function(e, authUser){

  var queryRef = ref.startAt(authUser.uid).endAt(authUser.uid);

  var queryArray = $firebase(queryRef).$asArray();

  queryArray.$loaded().then(function() {

    setCurrentUser(queryArray.$keyAt(0));

  });
});

      

The returned data should be one item from firebase, but the queryArray is empty when I use console.log for debugging.

Without using startAt and endAt, the queryArray contains all the elements stored in firebase. Therefore, registering queryArray. $ keyAt (0) gives the name First elements as output. As expected.

I also noted the release notes for Firebase 8.0 and I don't see any changes to these restrictions.

Please indicate if there is any syntax error or any alternative solution that can achieve the desired result.

Basically I want a single entry from Firebase which is my current user, authUser is an authenticated user with authUser.uid as priority.

Below is the JSON file that is populated in Firebase when a user logs in.

{
  "users" : {
    "User A" : {
      "md5_hash" : "d10ca8d11301c2f4993ac2279ce4b930",
      "setPriority" : "simplelogin:69",
      "username" : "User A"
    },
    "User B" : {
      "md5_hash" : "2076105f6efe7c11e285add95f514b9a",
      "setPriority" : "simplelogin:70",
      "username" : "User B"
    },
    "User C" : {
      "md5_hash" : "a6d14de05d7b2c3cf4fae7ae14cfa7f3",
      "setPriority" : "simplelogin:71",
      "username" : "User C"
    }
  }
}

      

After editing

Using the following code to get the priority:

queryRef.once('value', function(nameSnapshot) {
  var val = nameSnapshot.getPriority();
  console.log("Priority is: " + val );
});

      

Log output:

Priority: null

Method used to add user to Firebase:

create: function (authUser, username) {
  users[username] = {
    md5_hash: authUser.md5_hash,
    username: username,
    setPriority: authUser.uid
  };

users.$update(username, {
    md5_hash: authUser.md5_hash,
    username: username,
    setPriority: authUser.uid
    //$priority: authUser.uid
  }).then(function () {
    setCurrentUser(username);
  });

}, // end of create method

      

+3


source to share


1 answer


It looks like the precedence for all your data is NULL. This prevents the correct operation of endAt

and startAt

.

The clue that something is happening is the presence of a key setPriority

in your data. Priority is metadata that is managed out of the ordinary.

Change the user creation code to the following:



create: function (authUser, username) {
  users[username] = {
    md5_hash: authUser.md5_hash,
    username: username,
    .priority: authUser.uid
  };

} // end of create method

      

or that:

create: function (authUser, username) {
  users[username] = {
    md5_hash: authUser.md5_hash,
    username: username
  };

users.$update(username, {
    md5_hash: authUser.md5_hash,
    username: username
  }).then(function (dataRef) {
    dataRef.setPriority(authUser.uid);
    setCurrentUser(username);
  });

} // end of create method

      

+1


source







All Articles