How get autoID of new object after calling push() in firebase? - firebase

It's simple. I have an object called obj and the reference called ref
when I do:
ref.push(obj, function(err){});
or
ref.push().set(obj, function(err){});
how do I get the auto generated ID of the recently saved object?

The push function returns a reference to the newly created object.
var newRef = ref.push();
console.log(newRef.name());
newRef.set(obj, function(err) {});
Note that in the above snippet no data is sent to the server until the call to set. The push() method is pure client-side, as long as you don't pass any data into it.
https://www.firebase.com/docs/web/api/firebase/push.html

Related

Firebase get user data during onCreate

After an user creation, I have difficult to retrived data in a onCreate method.
exports.accountCreate = functions.auth.user().onCreate(user => {
console.log("--->"+user.data);
console.log("ok");
return true;
});
But I received an undefined value
As you can see in the API documentation, the onCreate callback method receives a UserRecord object as its first argument. Your function is calling it user, and is trying to access a property called data on it. But you can see that UserRecord doesn't have a data property. It has lots of other properties, so try one of them instead. Or maybe call its toJSON method to generate an object that you could also log.

Not able to do a console.log in .then() after adding an object into firebase array

I'm binding my firebase array to data variable and I'm adding an object into this array using $add but I'm unable to get an output from .then()
$scope.dbref =firebase.database().ref().child("Employee");
$scope.data = $firebaseArray( $scope.dbref);
var obj = {};
obj.name=$scope.username;
obj.age=$scope.age;
obj.gender=$scope.gender;
obj.active=$scope.active;
$scope.data.$add(obj).then(function(value){
console.log('something');
console.log(value.$id);
});
I'm sorry it was just that angular fire uses 3.6.6 and i was using 3.7.1

AngularFire / Firebase - How Do You Delete Key That is More Than One Level Deep?

I have an synced object that is initialized like this:
var sync = $firebase(ref);
var syncObject = sync.$asObject();
syncObject.$bindTo($scope, 'data');
The synced object has the following structure:
{
levelone: {
leveltwo: {
valueone: 1
valuetwo: 2
}
}
}
I would like to remove one of the values with $remove(), but I am not able to delete anything other than the entire object with this method.
The following will delete the entire object:
syncObject.$remove('levelone.leveltwo.valueone');
I do not want to use $save() to overwrite the entire synced object.
Is there anyway to remove a nested key with $remove()?
There is no way to drill through multiple layers with the $remove() function. I ended up using vanilla Firebase JS library methods to do the delete.
var ref = new Firebase(myFirebaseURL);
// The child() method (from the vanilla JS library) allows you to target keys
// more than one level deep
ref.child('levelone/leveltwo/valueone').remove();
var sync = $firebase(ref);
You can chain the child() function off of the original JS Firebase class object and then run the remove() method off of that (notice that this is the remove() method and not the $remove() method).

Iterating over $firebaseArray and $firebaseObject

I am trying to iterate over firebaseObject and firebaseArray fetched from my Firebase but they don't seem like normal javascript objects and arrays.
My data is stored in the following form
'mainKey': {
'key1':'value1',
'key2':'value2'
},
'mainkey2': {
'key3':'value3'
}
I've tried the following code
var firebaseRef = new Firebase("https://<my-app>.firebaseio.com/);
var fbArray = $firebaseArray(firebaseRef);
var fbObject = $firebaseObject(firebaseRef);
for(var i=0;i<fbArray.length;i++){
console.log(fbArray[i]);
}
console.log(fbObject);
console.log(fbObject.mainkey);
console.log(fbArray.length);
This gives the following output in the console
Object { $$conf={...}, $id="test", $priority=null, more...}
undefined
0
Though the object returned has mainkey property but I'm not able to access it.Why does this happen? And how should I iterate over them ?
You could try for..in loop to iterate over an object.
Below is an example:
for (var key in fbObject) {
console.log(fbObject[key]); // You could use this method with all objects in JS
}
here's the info you need to know
The $firebaseArray service takes a Firebase reference or Firebase Query and
returns a JavaScript array which contains the data at the provided Firebase
reference. Note that the data will not be available immediately since
retrieving it is an asynchronous operation.
You can use the $loaded() promise to get notified when the data has loaded.
https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-firebasearray
fbArray.$loaded(function() {
//here you can iterate over your object
});

When utilizing the .push method can I write a copy of the id to the object?

I'm using the .push method on firebase to write new records. I'd like to save the key where the new record is saved to the record itself at the id key. Currently, I do this in 2 operations, first push the record and then update using the ref returned. Can I do this in 1 write? Does it not matter?
If you invoke the Firebase push() method without arguments it is a pure client-side operation.
var newRef = ref.push(); // this does *not* call the server
You can then add the key() of the new ref to your item:
var newItem = {
name: 'anauleau'
id: newRef.key()
};
And write the item to the new location:
newRef.set(newItem);
There's no method to do this in one operation. However, it typically does not matter, because you can always get the push id from the .key() method on the DataSnapshot.
But, there's nothing wrong either about storing the push id. So you coul create a function on the Firebase prototype.
Firebase.prototype.pushWithId = function pushWithid(data) {
var childRef = this.push();
data.key = childRef.key();
childRef.update(data); // or .set() depending on your case
return childRef;
};
var ref = new Firebase('<my-firebase-app>');
ref.pushWithId({ name: 'Alice' });
Take caution with modifying the prototype of functions you do not own. In this case, you'll likely be fine. This method does little, and there's not much of a chance that the Firebase SDK gains a .pushWithId() method.

Resources