Lloyd Banks
Lloyd Banks

Reputation: 36648

Setting Your Own Key When Adding an Object to Firebase - AngularFire

I can use the following to add an object to my Firebase data store:

var uniqueId = {
    name: "a name",
    location: "new york"
}
$scope.myItems.$add(uniqueId).then(function(firebaseId){
    // do something on success
}, function(){
    // do something if call fails
});

The above will add an object into my data store and if the add is successful, an ID generated by Firebase is returned. The object I just added is saved under this key.

Is there a way for me to specify what the key name is when I add to my data store?

Upvotes: 14

Views: 28354

Answers (4)

Asaph Souza
Asaph Souza

Reputation: 1

.set still working in angularfire api

So, try this...

var gT = $scope.ultima[0].guia + 1;                    
var _db = firebase.database().ref('/guias/' + gT);
                      _db.set({
                    'artigo': _dt.artigo,
                    'inicio': $scope.ultima[0].fim + 1,
                    'guia': $scope.ultima[0].guia + 1,
                    'fim': $scope.ultima[0].fim + 1 + (1 * _dt.quantidade),
                    'quantidade': _dt.quantidade,
                });

Upvotes: 0

Lee
Lee

Reputation: 241

You can try it like this:

var empsRef = ref.child("employees");

empsRef.child('11111').set({
  lastname: "Lee",
  firstname: "Kang"
});

empsRef.child('22222').set({
  lastname: "Nut",
  firstname: "Dough"
});

The output should look like this:

"employees" : {
  "11111" : {
    "lastname" : "Lee",
    "firstname": "Kang"
  },
  "22222" : {
    "lastname" : "Nut",
    "firstname": "Dough"
  }
}

Upvotes: 9

Alex
Alex

Reputation: 842

It looks like $child and $set have been removed from the AngularFire API.

Here is my workaround.

  1. Listen for $onAuth
  2. Create a new $firebaseObject with the ref as a child of users and the id as the uid (in this case, facebook)
  3. Save the user object

    angular.module("app.services", []).factory("Auth", function ($firebaseAuth, $firebaseObject, FirebaseUrl) {
    var auth = $firebaseAuth(new Firebase(FirebaseUrl));
    
    auth.$onAuth(function (authData) {
        if (authData) {
            var ref = new Firebase(FirebaseUrl + "/users/" + authData.uid);
            var user = $firebaseObject(ref);
            user.name = authData.facebook.displayName;
            user.$save().then(function () {
                console.log(user);
            });
        }
    });
    return auth;
    })
    

I am still struggling to make this testable though...

Upvotes: 3

David East
David East

Reputation: 32604

Everything in Firebase is a URL.

Take the following URL for example.

https://myapp.firebaseio.com/users/

Let's say we want to create a user with a key of 1 as a child at this location. Our URL would look like this.

https://myapp.firebaseio.com/users/1

To create a user using AngularFire we can create a reference at the users node and call $child(1) to create a reference to that location.

var usersRef = new Firebase('https://myapp.firebaseio.com/users');
var userRef = new Firebase('https://myapp.firebaseio.com/user/1');

$scope.users = $firebase(usersRef);

// these are the same
$scope.userOne = $firebase(userRef);
$scope.userOne = $scope.users.$child(1);

Then we can use $set to store the value of the user at that location.

var usersRef = new Firebase('https://myapp.firebaseio.com/users');
$scope.users = $firebase(usersRef);
$scope.users.$child(1).set({
  first: 'Vincent',
  last: 'Van Gough',
  ears: 1
});

In your case it would be:

var uniqueId = {
    id: 1,
    name: "a name",
    location: "new york"
};
$scope.myItems.$child(uniqueId.id).$set(uniqueId);

Remember that using $set will destroy any previous data at that location. To non-destructively update the values use $update.

Upvotes: 17

Related Questions