Currently I am using:
this.af.database.list('users').push(user);
How can I make the key of the object that is submitted be custom. So 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 Id I just need to be able to know how to make the custom user objects node not be the auto generated id when pushing.
Thanks
Use a method like below with Angularfire2
addCustomKey (key, value) {
const toSend = this.af.database.object(`/users/${key}`);
toSend.set(value);
}
Here is another method you can use with AngularFire2
addCustomKey(key, value) {
const users = this.af.database.object('/users');
users.update({ [key]: value });
}
push
will add a push key, 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);
As mentioned in many of the answers above, calling push()
will generate a key for you. Here's an alternative approach.
Instead, you have to use child()
if you want set your custom key to determine the key/path for yourself. Like so:
firebase.database.ref().child('/path/with/custom/key/goes/here').set();
In your case, to use the user's authentication ID, here's how you approach it:
export class SomeClass {
uid: string;
constructor(public afDB: AngularFireDatabase, public afAuth: AngularFireAuth) {
// Get User Auth ID
afAuth.authState.subscribe(user => {
this.uid = user.uid;
});
}
//Your method
this.afDB.database.ref().child(`users/${this.uid}/`).set(user);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With