Reputation: 36648
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
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
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
Reputation: 842
It looks like $child and $set have been removed from the AngularFire API.
Here is my workaround.
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
Reputation: 32604
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