makkasi
makkasi

Reputation: 7288

When to use uid and when to use $id in angularfire

$id The key where this record is stored. The same as obj.$ref().key

To get the id of an item in a $firebaseArray within ng-repeat, call $id on that item.

These two are from the angular fire reference: https://github.com/firebase/angularfire/blob/master/docs/reference.md

What I understand is if there is firebase object created with :

var object = $firebaseObject(objectRef);

then I can use uid always.

uid : object.uid

But I saw examples where the firebase auth user is used with $id.

return Auth.$requireSignIn().then(function (firebaseUser) {
                        return Users.getProfile(firebaseUser.uid).$loaded().then(function (profile) {
               **profile.uid or profile.$id here**

Also is it possible the object to have uid but not to have $id (obj.$ref().key). Aren't they the same thing? Does the object have to be loaded first with $loaded() to use $id or uid?

Regards

Upvotes: 1

Views: 440

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598740

You seem to be confusing two concepts:

  • the object.$id of an AngularFire object contains the key of that object in the Firebase Database.

  • a firebaseUser.uid in Firebase terms is the identification of a Firebase Authentication user.

It is common to store your Firebase Authentication users in the database under their uid, in which case user.$id would be their uid. But they are still inherently different things.

Users
   uid1
      displayName: "makkasi"
   uid2
      displayName: "Frank van Puffelen"

So if you we look at the code snippet you shared:

return Auth.$requireSignIn().then(function (firebaseUser) {
    return Users.getProfile(firebaseUser.uid).$loaded().then(function (profile) {

The first line requires that the user is signed-in; only then will it execute the next line with the firebaseUser that was signed in. This is a regular JavaScript object (firebase.User), not an AngularFire $firebaseObject.

The second line then uses the firebaseUser.uid property (the identification of that user) to load the user's profile from the database into an AngularFire $firebaseObject. Once that profile is loaded, it executes the third line.

If the users are stored in the database under their uid, at this stage profile.$id and firebaseUser.uid will be the same value.

Upvotes: 3

Related Questions