TheUnreal
TheUnreal

Reputation: 24472

Angularfire2 add a new user to the database

I used the following login method:

login() {
  this.auth.login();
this.authService.login().subscribe(() => {
  if (this.authService.isLoggedIn) {
      console.log("ADDING THE USER..");
      // Add a new user to the users table
      this.af.child("users").child(this.authService.userData.uid).set({
          provider: this.authService.userData.provider,
            name: this.getName(this.authService.userData)
      });
    // Redirect the user
    this.router.navigate(['/panel']);
  }
});

}

Which almost same as the method in the documentation: https://www.firebase.com/docs/web/guide/user-auth.html#section-storing

When I run my app, I get the following error:

Property 'child' does not exist on type 'AngularFire'.

and the user is not added. What's wrong?

All I need is to add a new object to my Firebase database. The following works, but I need the structure as shown above:

this.users = this.af.database.list('/users');
this.users.push("TEST");

So, How I can add a new user object into my database using the same structure in https://www.firebase.com/docs/web/guide/user-auth.html#section-storing? and why I can't run my code if it's same as documented?

Update

My new code:

this.af.database.object('/users/${this.authService.userData.uid}').set({
              provider: this.authService.userData.provider,
                name: this.getName(this.authService.userData)
          });

Upvotes: 1

Views: 2241

Answers (2)

Calvin Ferrando
Calvin Ferrando

Reputation: 4082

This one is a shorter version.

import { AngularFireDatabase } from "angularfire2";

...

constructor(private afd: AngularFireDatabase) {}

...

this.afd.object(`users/${this.authService.userData.uid}`).set({...});

Upvotes: 0

Sasxa
Sasxa

Reputation: 41264

Use object(); child() doens't exist:

this.af.database.object(`users/${this.authService.userData.uid}`).set({...})

Upvotes: 4

Related Questions