AngularFire2 how to use your own private key instead of automatically generated

I am currently using:

this.af.database.list('users').push(user); 

How to make the key of the object that was sent is normal. Therefore, I want to be able to set the node of this object to the same uid of the registered user. I have access to this identifier. I just need to know how to make the user objects of the user node not an auto-generated identifier when clicked.

thanks

+6
source share
6 answers

Use a method like below with Angularfire2

 addCustomKey (key, value) { const toSend = this.af.database.object(`/users/${key}`); toSend.set(value); } 
+9
source

Here is another method you can use with AngularFire2

 addCustomKey(key, value) { const users = this.af.database.object('/users'); users.update({ [key]: value }); } 
+4
source

push a button key will be added, just use the update method:

 this.yourRef = this.af.database.list('users'); let userId = "UID", userInfos = { data: "your user datas" }; this.yourRef.update(userId, userInfos); 
+2
source

vanilla js looks something like this:

 //to generate a key do something like var id = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); return v.toString(16); }); function setNode(key){ if (key){ var thisRef = ref.child('testLocation'); var obj = {}; obj[key] = true; thisRef.update(obj, onComplete); } } 
+1
source

Go to your user key, in this case projectKey, which is a variable that you populate somewhere:

 private whatEverName(projectKey) { this.projectKey = projectKey; this.af.object('/Projects/' + this.projectKey); } 

Call whatEverName (projectKey) in your code somewhere, and you have a custom node key. This tip is valid from July 25, 2017.

I added a bit more to help those trying to create a child of the node, with the one above, with another key as a custom key. This is useful for the association index for this project, such as a list of users working on this project. You may not need this list in the project data, but get it on request. It simplifies data denormalization and has a flatter architecture, the goal of Firebase.

In this case, the node association is called ProjectsMembers, and we write the owner of the project into it. Add other custom nodes in the same way. Pay attention to the backlinks !!! Single quotes will not work. In this case, I set the name of the owner as a link to the keystroke. Easier to read in db.

 // Add child node with project key as push key to the association index. this.af.object(`ProjectsMembers/${projectKey}/` + this.ownerKey) .set({ ownerName: this.ownerName }); 
+1
source

Just do it with the usual firebase method as follows:

 var updates = {}; updates['/users/' + userId] = newUserData; return firebase.database().ref().update(updates); 

Anything related to your area associated with your list will reflect an update.

Read and write data online

0
source

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


All Articles