Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

startAt and endAt not working as expected using angularfire : 0.8.0

Using AngularFire 8.0

Bellow is the code which uses startAt and endAt to limit the 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 data returned should be a single element from firebase but queryArray is empty when I use console.log for debugging.

Without the use of startAt and endAt, the queryArray contains all the elements stored in the firebase.Therefore, logging queryArray.$keyAt(0) gives the First elements name as output. Which is as expected.

I have checked the release notes of Firebase 8.0 as well and I don't see any changes in these limiters.

Please point out if any syntactical mistake or any alternative solution which can achive the desired result.

I basically want single record from the Firebase, which is my current user , authUser is the authorized user with authUser.uid as its priority.

Following is the JSON file which is populated in the Firebase when a user registration happens.

{
  "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 Edit

Using the bellow code to get the priority:

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

Log output is:

Priority is: null

The method used to add user to the Firebase is:

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
like image 761
Div Sehgal Avatar asked Nov 29 '25 18:11

Div Sehgal


1 Answers

It looks like the priority on all of your data is null. This prevents endAt and startAt from working properly.

The clue that something is up is the existence of the setPriority key in your data. Priority is metadata that's managed outside the normal view.

Change your user creation code to something like this:

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

} // end of create method

or this:

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
like image 86
mimming Avatar answered Dec 01 '25 07:12

mimming