How to set key name in array in Angularfire

I am creating arrays using Angularfire, but I cannot set the key name. Firebase automatically gives me a secret key (for example, Jm9vLy8Lye-KS35KsmL), but I would like to set it myself to something more meaningful. It is unclear how I do this in Angularfire. I use the $ add method on $ firebaseArray:

var firebaseRef = new Firebase("https://firebase_location"); $scope.messages = $firebaseArray(firebaseRef); $scope.messages.$add( { FirstName: patient.FirstName, LastName: patient.LastName } ).then(function(firebaseRef) { var id = firebaseRef.key(); }); 

The data is kept in order and I can see it on the toolbar. However, id is always a cryptic firebase value. I wish I could establish something meaningful myself. In my case, the individual patient identifier would be significant ...

Any thoughts?

Thanks!

+6
source share
3 answers

Thanks for the answer.

I found that the child function will do what I need. First I point out the child and then set it.

 var patient_child = firebaseScreenRef.child(patient.PatientId); patient_child.set( { FirstName: patient.FirstName, LastName: patient.LastName, }); 
+1
source

Adding an element to the firebase array makes firebase to determine a unique identifier. If this is not what you want, I think you can try to get the "messages" object using $ firebaseObject, instead of just getting a list with $ firebaseArray. Then you can edit your object in js to add elements to the message collection. This way you can use the identifier that best suits your needs. Finally, you must $ save () your entire object. See here: https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-firebaseobject

Hope this helps.

0
source

For your question, I found an explanation. You do not need to use firebaseObject, you must use ref directly:

 var ref = new Firebase(FURL); createProfile: function(id ,user) { var profile = {`enter code here` name: user.name, email: user.email, gravatar: get_gravatar(user.email, 40) }; var profileRef = $firebaseArray(ref.child('profile').child(id)); return ref.child('profile').child(id).set(profile); }, 

In the code, I use ref to link to my URL. With profileRef, I created a child profile, and I added an id for the profile. Subsequently, I use ref directly to set the profile value for the identifier I want. You see, it is very easy.

0
source

Source: https://habr.com/ru/post/984711/


All Articles